排球setRetryPolicy超时不起作用
问题描述:
我想将我的应用程序超时时间设置为60秒,这意味着我的应用程序只会在ProgressDialog从服务器获取答复或达到超时但未从服务器获得答复时关闭ProgressDialog。排球setRetryPolicy超时不起作用
目前我与排球库工作在Android上,所以这是我做的:
private void loginOnline(final String user, final String pwd, final String login_url){
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Communicating with Server");
pd.show();
final RequestQueue queue = Volley.newRequestQueue(this);
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, user);
params.put(KEY_PASSWORD, pwd);
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, login_url, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pd.dismiss();
try {
int msg = response.getInt("status");
sendMessage(msg);
}
catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
Log.d("D", "onErrorResponse: "+error.getMessage());
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjReq);
}
问题是,当我尝试连接到我的服务器,它显示在日志中:
D/D: onErrorResponse: java.net.ConnectException: failed to connect to /192.123.x.xxx (port 3000) after 60000ms: isConnected failed: EHOSTUNREACH (No route to host)
D/Volley: [1] Request.finish: 3072 ms: [ ] http://192.123.4.215:3000/login 0xdde27c7c NORMAL 1
我的问题是为什么它停止连接到服务器之前,它达到60000毫秒。 在此先感谢
答
因为它不只是无法连接到主机 - 它无法找到它的路由。这意味着它永远不会与主持人通话。因此,它立即返回。重试策略正在工作,但仅适用于连接可能的情况。如果服务器拒绝连接或其他一些条件,它也会立即结束。
感谢您的回答! – VincentTheonardo