10

I am trying to do a POST request from Android to insert some information in my phpmyadmin.

What I am using

  • Slim to make the connection and the query with the database stored at phpmyadmin.
  • XAMPP to simulate a local server.
  • Volley to consume the requests from Android.

The Slim post function is not giving any problems to me because if I use it with POST mode on Postman application the data is being inserted properly on the database.

The url can accept two parameters separated by slash /. Here one example:

http://localhost:8000/insert/Peter/25

in which I insert a new user with name Peter and age of 25 in the database.

The problems come when I try to call the same url from Android using Volley because it always goes to onErrorResponse method.

This is the code that I have to consume the request from Android:

RequestQueue queue = Volley.newRequestQueue(getContext());

String url = "http://localhost:8000/insert/" + name + "/" + age;
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
     new Response.Listener<String>()
     {
         @Override
         public void onResponse(String response) {
            Log.d("Response", "works well");
         }
     },
     new Response.ErrorListener()
     {
         @Override
         public void onErrorResponse(VolleyError error) {
            Log.d("Response", "" + error.getMessage());
         }
     }
);

queue.add(postRequest);

But it always goes to the onErrorResponse method.

What I have checked

  • That the computer in which I have the XAMPP running is on the same WIFI than the smartphone.
  • That changing localhost for the IP of my computer also does not change the result.

UPDATE:

I have noticed that I was using the IP of Ethernet adapter instead of WIFI on the url.

Now I can see on smartphone browser XAMPP default webpage if I set http://192.168.x.x (IP of my WIFI) on the url but I still have the problem that I mentioned above because if I set http://192.168.x.x:8000/insert/Peter/25 the url is not recognised by the smartphone.

I think that the problem could be because I use the built-in PHP server as Slim documentation suggests. I use the following command:

php -S localhost:8000

so I think that the problem can be generated here but I am not sure how to solve it.

What am I missing?

Thanks in advance!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • 1
    It may be that the built-in web server is not setup to be accessible from outside of the host computer. Try changing the bind address to `0.0.0.0` and see what happens. – meun5 Feb 21 '17 at 13:00

5 Answers5

5

Finally I notice what my problem was. I am going to share the points that I struggled on.

  • First of all, I did not need to use the built-in PHP server because it was not neccesary.

  • Second, and most important thing, is that I also had to use the name of the file in which Slim functions are stored (custom functions that I have created) on the url.

  • Third, that I had to use the IP of the computer in which XAMPP was running so the mobile phone could access to it instead of localhost (that refered to the localhost of mobile phone so it will never work).

  • Fourth, that both devices (mobile phone and computer) were connected to the same WIFI network so when you run your XAMPP in your computer the mobile phone could access to it.

So the final url I had to use was:

http://192.168.x.x/application/index.php/insert/Peter/25

where you have to replace 192.168.x.x with the IP of the WIFI network of your computer and application with the name of the folder in which you are using Slim.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
4

You can do like this also

StringRequest postRequest = new StringRequest(Request.Method.POST, url,
     new Response.Listener<String>()
     {
         @Override
         public void onResponse(String response) {
            Log.d("Response", "works well");
         }
     },
     new Response.ErrorListener()
     {
         @Override
         public void onErrorResponse(VolleyError error) {
            Log.d("Response", "" + error.getMessage());
         }
     }

@Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

@Override
    protected Map<String, String> getParams() 
    {  
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("name", "Atif");  
            params.put("age", "27");
            return params;  
    }

);
AbS
  • 71
  • 1
  • 6
3

You are binding the internal PHP webserver to localhost ie. 127.0.0.1. It cannot be accessed from any external device. To bind the PHP webserver to your external ip address of 192.168.x.x start it with.

$ php -S 192.168.x.x:8000

Be sure to replace the .x.x part with the actual numbers of your ip address. You must also make sure your Android device is connected to same 192.168.x network.

After this is done your Android device should make the request to 192.168.x.x:8000. Now it is trying to connect to localhost ie. itself.

Unless you are running the PHP webserver in your Android device port 8000 connecting to localhost:8000 will not work.

Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
2

Instead of passing name and age to the url pass them like this(for that make sufficient changes in your web service)

    @Override
    protected Map<String, String> getParams() 
    {  
            Map<String, String>  params = new HashMap<String, String>();  
            params.put("name", "Atif");  
            params.put("age", "27");

            return params;  
    }

See detail example here

AbS
  • 71
  • 1
  • 6
  • I will take a look at it but do you know why I cannot send the data on the url? I mean if there is some specific reason. I think that it should be something about security but I am not secure about that. – Francisco Romero Feb 19 '17 at 17:53
  • 1
    It is giving to me the following error: `java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8000)` – Francisco Romero Feb 20 '17 at 21:24
  • 1
    If you are referring your localhost on your system from the Android emulator then you have to use http://10.0.2.2:8000/. Look at this [link](http://stackoverflow.com/questions/5495534/java-net-connectexception-localhost-127-0-0-18080-connection-refused) – AbS Feb 21 '17 at 04:25
  • No, I refer to mobile phone as I pointed on my question, not an emulator. You can see my `Update` section to see my advances. – Francisco Romero Feb 21 '17 at 08:07
  • try [this](https://www.webucator.com/blog/2016/05/accessing-your-local-web-server-from-a-mobile-device-using-xampp/) hope your problem get solved – AbS Feb 21 '17 at 12:49
1

localhost can be used for the same sytem from which the xampp is started. If you want to connect it through some other device like android phone, you need to put the Public IP Address, go to Command Prompt and type

ipconfig

Check the IPv4 Address for the public IP.