8

I have a native android app using volley framework to fetch data from a PHP server end script.

It worked well on most time, but I have 20% percentage failure.

The error says:

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

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

I have no idea what can be the reason? Since it is working most time so there should be no obvious error code.

FYI, those PHP script on the server end works very well for my IOS app.

Please allow me post 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();
        }
    });


}

And for more information, the output of my PHP script is: {"hsaBalance":"1000.00"}, created by Json_encode() function of PHP.

1

3 Answers 3

22

I have fixed this bug. It is not a network issue.

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

should be

queue.add(getRequest);

So the key is we should remove queue.start().

Sign up to request clarification or add additional context in comments.

Comments

4

Michael Cheng is right,because volley had start the RequestQueue when we call newRequestQueue as below:

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;
}

and when we call start, volley will call stop to “make sure any currently running dispatchers are stopped”,in stop method volley does this below:

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

and the quit method does this below:

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

maybe you can see the reason,why interrupted.
More, interrupt method does this below:

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();
        }
    }
}

the reason maybe this as metioned above:

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.

1 Comment

This makes sense, @xiaoyee. Ran into intermittent issues around here. Thanks for the help.
1

You are having sometimes problems with your connection. Look at InterruptedIOException API:

InterruptedIOException Signals that an I/O operation has been interrupted. An InterruptedIOException is thrown to indicate that an input or output transfer has been terminated because the thread performing it was interrupted.

so only you can do is to catch the possible exceptions occuring when converting JSon and having 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();
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.