-2

I am trying to POST a value to a script running on a local server. My code is given below :

 final String data = "poet=nazmul";
        //Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
        StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.1.112:8080/api/post.php?" + data, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), volleyError.toString(), Toast.LENGTH_LONG).show();
            }
        });
        AppController.getInstance().addToRequestQueue(request);

But the local server does not respond. My php code is:

<?php
 $poet = $_POST['poet']; 
 echo json_encode(array('poet'=>$poet));
?>

When I open the URL directly in a web browser, I get this error:

Notice: Undefined index: poet in C:\xampp\htdocs\api\post.php on line 2 {"poet":null}

How can I solve this problem? The main problem was windows firewall, it was preventing me from connecting to the local server, after disabling it and changing the php code $_POST to $_GET everything seems working fine.Thanks everyone for their help.

9
  • no, i am trying from real device Commented Aug 3, 2015 at 8:07
  • This might be a stupid question, but can you open a web browser to that URL and get the JSON response you expect? Commented Aug 3, 2015 at 8:09
  • First you may use a web browser to test your URL Commented Aug 3, 2015 at 8:11
  • when i post the url in my pc browser : this output comes Notice: Undefined index: poet in C:\xampp\htdocs\api\post.php on line 2 {"poet":null} Commented Aug 3, 2015 at 8:12
  • You mean that you are connected your phone to PC and you want to communicate with php script on your computer? Commented Aug 3, 2015 at 8:15

2 Answers 2

1

You're doing a get request because of the concatenation of ? and + data. If you change the server script $_POST to $_GET and change the Request.Method.POST to Request.Method.GET this will work.

Sign up to request clarification or add additional context in comments.

1 Comment

You can use URL parameters with POST as well as GET. If the method call specifies POST, then POST is what he is using.
1

You're submitting data to your script using POST (which doesn't matter in this case, but if it was changing something -- not idempotent -- you must do). Typically, data submitted using POST will have a request body that is sent along with the request, and not using URL parameters as you have here.

Both are fine, but unfortunately PHP's $_POST dictionary only gets values from the message body, and not the URL parameter (which $_GET references). To access them, you will need to parse the request URI yourself, by accessing the $_SERVER['REQUEST_URI'] variable.

If you are never going to make changes to your server's state with this request, it is appropriate to change the request type to GET. Please read this question and answers for more insight into the differences between GET and POST and using URL parameters.

Alternatively, if you don't want to change the PHP script at all, you need to modify your Android code to send the data in the request body (and not as a URL parameter). Please see this question on some approaches to doing that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.