Reputation: 1009
I currently use HttpClient/HttpResponse calls in my android app to transfer and receive data to my wordpress site and it works just fine. I use https://wordpress.org/plugins/json-api/ plugin on the site to handle the requests. Google is trying to phase out those http protocols and suggests using volley for networking instead. So i'm trying to make the transition but the query post parameters to the server gets rejected somehow.
Using code from here as a starting point: http://code.tutsplus.com/tutorials/creating-a-weather-application-for-mars-using-volley--cms-23812
private String WORDPRESS_API_ENDPOINT = "http://example.com/api/mobileapp/get_posts?id=123&title=test"; // example url with query parameters
CustomJsonRequest request = new CustomJsonRequest
(Request.Method.POST, WORDPRESS_API_ENDPOINT, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// Do stuff with the returned data
} catch (Exception e) {
txtError(e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
txtError(error);
}
});
request.setPriority(Request.Priority.HIGH);
helper.add(request);
}
On my server php code, i'm printing the $_SERVER(the $_POST and $_GET are empty) global variable to see incoming requests and shows the following(only showing relevant entries in the array):
array (
'REQUEST_URI' => '/api/mobileapp/get_posts/',
'REQUEST_SCHEME' => 'http',
'CONTEXT_PREFIX' => '',
'REMOTE_PORT' => '13326',
'REDIRECT_URL' => '/api/mobileapp/get_posts/',
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => ''
)
Notice the Request_uri is stripped out of the query parameters and i'm not able to get the right post that i need. I've tried debugging the volley code quite a bit and read through a lot of SO posts on passing the right json data the right way through volley, but none of the approaches works. The error it appears seems to be somewhere on the server side as, if i use a different api url (flickr for example), i'm able to make successful calls. What changes do i need to make to my server to recieve volley calls as compared to the httpclient calls.
Also, while debugging the volley calls, i noticed under HurlStack.java performRequest function:
int responseCode = connection.getResponseCode();
The url is stripped out at this point from the 'connection' variable right after the line is executed.
Also, if i copy paste the url into a browser, it shows me the data i'm expecting as it reads the query parameters.
Upvotes: 0
Views: 774
Reputation: 1009
So, this was only happening on GENYMOTION emulator. The parameters get passed correctly when i use the built in emulator and an actual device.
Found another person facing a similar situation on genymotion: Volley request not taking parameters
So, anyone else using volley and happened to be using genymotion, you may have to switch your emulators (for now at least).
Upvotes: 1