Random error com.android.volley.NoConnection, java.io.InterruptedIOException, statuscode = 0

I have my own Android application using the volley framework to receive data from the end of the server PHPscript.

It worked well in most cases, but I have a 20% failure.

The error says:

com.android.volley.NoConnection, java.io.InterruptedIOException.

I debugged what I found statuscode = 0, which was obviously wrong.

I have no idea what could be causing? Since it works the most time, there should be no obvious error code.

FYI, those PHP scripts on the server work fine for my iOS application.

Please let me write my code here:

retryConnBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtOut.append("\n");
            txtOut.append("Button with Retry Click");
            Log.d("Click", "Button Click");
            final String url = "https://www.myserver.com/api/getBalanceInfoTest?token=7ff3317a4f3dc07d0c297a7d16d2049c&t=" + System.currentTimeMillis();
            //final String url = "http://192.168.1.23/base/test/";
            JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            txtOut.append("\n");
                            txtOut.append("Result with Retry:");
                            txtOut.append(response.toString());
                            Log.d("Response", response.toString());
                            VolleyLog.e("Response:", response.toString());
                        }
                    },
                    new Response.ErrorListener(){
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            txtOut.append("\n");
                            txtOut.append("Error with Retry:");
                            txtOut.append(error.toString());
                            Log.d("Error.Response", error.toString());
                            VolleyLog.e("Error:", error.getMessage());
                        }
                    });

            getRequest.setRetryPolicy(new DefaultRetryPolicy(5000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            queue.add(getRequest);
            queue.start();
        }
    });


}

PHP script: {"hsaBalance":"1000.00"}, Json_encode() PHP.

+4
3

. .

queue.add(getRequest);
queue.start();

queue.add(getRequest);

, queue.start().

+10

, RequestQueue, newRequestQueue, :

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

    String userAgent = "volley/0";
    try {
        String packageName = context.getPackageName();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
        userAgent = packageName + "/" + info.versionCode;
    } catch (NameNotFoundException e) {
    }

    if (stack == null) {
        if (Build.VERSION.SDK_INT >= 9) {
            stack = new HurlStack();
        } else {
            // Prior to Gingerbread, HttpUrlConnection was unreliable.
            // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
            stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
        }
    }

    Network network = new BasicNetwork(stack);

    RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
    queue.start();

    return queue;
}

start, , ", ", volley :

public void stop() {
    if (mCacheDispatcher != null) {
        mCacheDispatcher.quit();
    }
    for (int i = 0; i < mDispatchers.length; i++) {
        if (mDispatchers[i] != null) {
            mDispatchers[i].quit();
        }
    }
}

quit :

 public void quit() {
    mQuit = true;
    interrupt();
}

, , .
, :

public void interrupt() {
    // Interrupt this thread before running actions so that other
    // threads that observe the interrupt as a result of an action
    // will see that this thread is in the interrupted state.
    nativeInterrupt();

    synchronized (interruptActions) {
        for (int i = interruptActions.size() - 1; i >= 0; i--) {
            interruptActions.get(i).run();
        }
    }
}

, :

, , , , .

+4

You are having trouble connecting. Check out the InterruptedIOExceptionAPI:

InterruptedIOException Signals that an I / O operation has been interrupted. Interruption InterruptedIOException indicates that the transfer of input or output has been completed because the thread executing it has been interrupted.

therefore, only you can do this to catch possible exceptions that occur during JSon conversion and a workaround for this.

// rest of your code...

final String url = "https://www.myserver.com/api/getBalanceInfoTest?token=7ff3317a4f3dc07d0c297a7d16d2049c&t=" + System.currentTimeMillis();

try {
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,

    // rest of your code...

    queue.add(getRequest);
    queue.start();
} catch (InterruptedIOException e) {
    // do something when fail print error, show a toast
    System.out.err("Error, connection interrupted" + e.getMessage());
    Toast.makeText(getApplicationContext(), "press button again",  Toast.LENGTH_LONG).show();
} 
+1
source

All Articles