-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.

nazmul
  • 367
  • 2
  • 14
  • no, i am trying from real device – nazmul Aug 03 '15 at 08:07
  • This might be a stupid question, but can you open a web browser to that URL and get the JSON response you expect? – Aaron D Aug 03 '15 at 08:09
  • First you may use a web browser to test your URL – Mousa Jafari Aug 03 '15 at 08: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} – nazmul Aug 03 '15 at 08:12
  • You mean that you are connected your phone to PC and you want to communicate with php script on your computer? – Mousa Jafari Aug 03 '15 at 08:15
  • @minion You have sent `poet` in URL, it means that you are using GET method but you've passed `Request.Method.POST` to StringRequest constructor – Mousa Jafari Aug 03 '15 at 08:22
  • @minion So if you change `Request.Method.POST` to `Request.Method.GET` it will work on your PC but I don't warranty to work on your phone unless you have configured your `xampp` to accept requests and also with this IP probably will not work. – Mousa Jafari Aug 03 '15 at 08:26
  • i have change the php code from $_POST to $_GET, now the url is getting the value , but local sever is not connecting , when i pass the value from app – nazmul Aug 03 '15 at 08:58
  • 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. – nazmul Aug 23 '15 at 09:31

2 Answers2

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.

Spikey21
  • 431
  • 2
  • 8
  • You can use URL parameters with POST as well as GET. If the method call specifies POST, then POST is what he is using. – Aaron D Aug 03 '15 at 08:20
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.

Community
  • 1
  • 1
Aaron D
  • 7,540
  • 3
  • 44
  • 48