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.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
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!
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
.
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;
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With