随机com.android.volley.NoConnection错误,java.io.InterruptedIOException,statuscode = 0

Mic*_*eng 7 java android android-volley

我有一个原生的Android应用程序使用volley框架从PHP服务器端脚本获取数据.

它在大多数时候运作良好,但我有20%的失败率.

错误说:

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

我调试了我发现了statuscode = 0,这显然是错的.

我不知道是什么原因?由于它工作时间最长所以应该没有明显的错误代码.

仅供参考,服务器端的那些PHP脚本适用于我的IOS应用程序.

请允许我在这里发布我的代码:

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


}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,我的PHP脚本输出是: {"hsaBalance":"1000.00"},由Json_encode()函数创建PHP.

Mic*_*eng 16

我已修复此错误.这不是网络问题.

queue.add(getRequest);
queue.start();
Run Code Online (Sandbox Code Playgroud)

应该

queue.add(getRequest);
Run Code Online (Sandbox Code Playgroud)

所以关键是我们应该删除queue.start().

  • 谢谢你是那个人! (2认同)

Jor*_*lla 1

有时您会遇到连接问题。看InterruptedIOExceptionAPI:

InterruptedIOException表示 I/O 操作已中断。抛出 InterruptedIOException 表示输入或输出传输已终止,因为执行该传输的线程被中断。

所以你唯一能做的就是捕获转换 JSon 时可能发生的异常并为此找到解决方法。

// 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();
} 
Run Code Online (Sandbox Code Playgroud)