I have a simple app that uses volley like this:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("VolleyController", "Received volley response: " + response);
callback.onCallback(OperationOutcome.SUCCESS, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// This is null.
NetworkResponse response = error.networkResponse;
Log.e("VolleyManager", "Received error response from volley: ", error );
callback.onCallback(OperationOutcome.FAILURE, null);
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
This is the exception I get:
Received error response from volley: com.android.volley.NoConnectionError: java.io.EOFException
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:98)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:202)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:119)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:798)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
I've found some other answers about this online but they didn't solve it. The request is not getting to the server.
Thank you very much.
EDIT
I have both the permission on the manifest:
<uses-permission android:name="android.permission.INTERNET" />
and Internet is working fine on the device.
The problem was due to the fact that the request I was sending was empty. I thought that Volley would convert it to the correct JSON representation({}
) by itself. Instead, I think the request was just a null
and this caused the exception. To fix this, I've changed the StringRequest
in a JSONObjectRequest
. This has a constructor which takes as an input a JSONObject
like this:
// This will instantiate an empty JSON request with the correct format.
new JSONObject("{}");
I still don't get why Volley doesn't do by itself and I'd like to understand just out of curiosity. Anyway, after this, I've decided to switch to Retrofit which is much more performant and simpler to set up.
Thank you everybody for your help!