NoConnectionError in android volley

7.2k views Asked by At

The following is my code. I get com.android.volley.NoConnectionError: java.io.InterruptedIOException for the first time and for the second time it works fine. There server response is also fine, No error in server side.

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

    JsonObjectRequest request = new JsonObjectRequest(URL, null,
            new Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject responseJsonObject) {
                    try {
                        if (responseJsonObject.has("user")
                                && !responseJsonObject
                                        .isNull("user")) {
                                user.jsonParser(responseJsonObject
                                    .getJSONObject("user"));
                        }
                    } catch (JSONException exception) {
                        Toast.makeText(context, "Error Occured",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }, new ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    progressDialog.dismiss();
                    Log.d("Volley Error", volleyError.toString());
                    Toast.makeText(context, "Connectivity Error",
                            Toast.LENGTH_SHORT).show();
                }
            });

    queue.add(request);
    progressDialog.show();
    queue.start();
2

There are 2 answers

0
matdev On

I had the same problem and I solved it by removing the following line

queue.start()
0
John Cummings On

I was having the same issue. The question Vamsi referenced has a workaround using OkHttp that you works if you use OkHttp as the stack in volley (see also the comments in that question on how to use OkHttp in volley).

However, here is another hacky workaround that doesn't involve using another library:

final RequestQueue requestQueue = getRequestQueue();

final class ErrorFirstTimeHack {
    boolean first_time = true;
    MyVolleyJsonObjectRequest request = null;
}
final ErrorFirstTimeHack errorFirstTimeHack = new ErrorFirstTimeHack();
final MyVolleyJsonObjectRequest request = buildRequestObject(
    Request.Method.GET,
    url,
    new Response.Listener() {
        @Override
        public void onResponse(Object response) {
            // normal success processing
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error instanceof com.android.volley.NoConnectionError) {
                if (errorFirstTimeHack.first_time) {
                    errorFirstTimeHack.first_time = false;
                    requestQueue.add(errorFirstTimeHack.request);
                    return;
                }
            }
            // normal error processing
        }
    }
);
errorFirstTimeHack.request = request;
requestQueue.add(request);