Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my StringRequest is going always at onErrorResponse method?

Tags:

android

slim

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!

like image 250
Francisco Romero Avatar asked Feb 19 '17 02:02

Francisco Romero


2 Answers

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.

like image 103
Francisco Romero Avatar answered Oct 18 '22 00:10

Francisco Romero


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;  
    }

);
like image 28
AbS Avatar answered Oct 17 '22 23:10

AbS