Reputation: 6213
I'm using Volley StringRequest
to post a query to my server and I seeing that in about 20% of cases, the request is successful but onErrorResponse
is called instead of onResponse
.
Here is my request code:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("App", "[MainActivity] post successful");
// run very important routine only when post is successful
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("App", "[MainActivity] failed to post");
if (error == null) {
Log.e("App", "no error");
}
else if (error.networkResponse != null) {
Log.e("App", String.valueOf(error.networkResponse.statusCode));
if(error.networkResponse.data != null) {
try {
Log.e("App", new String(error.networkResponse.data, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
else {
Log.e("App", "no network response");
}
}
});
requestQueue.add(stringRequest);
20% of the time I see:
E/App: [MainActivity] failed to post
E/App: no network response
but in my server logs I see a 200 message and the data created by the post is in my database.
Why would Volley throw an error on a successful post?
UPDATE
Log.e("App", "error message: " + error.getMessage());
prints: E/App: error message: null
Upvotes: 3
Views: 432
Reputation: 466
What is the error message returned by volley? Check it by volleyError.getMessage()
.
Upvotes: 1