本文介绍了android volley库在蜂窝数据连接中给出超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即时通讯使用Android排球库为我的应用程序和当我使用wifi工作但它似乎有蜂窝数据连接的问题。在一些手机(LG G3 Android 6,魅族Android 5.1)与蜂窝数据它不起作用并给出超时错误但在(HTC欲望816 android 4.4,LG g3安卓5.0)再次工作
im using android volley library for my application and when im using wifi its working but it seems to have problem with cellular data connection. in some phones(LG G3 Android 6,Meizu android 5.1) with cellular data it doesnt work and gives timeout error but in (HTC desire 816 android 4.4, lg g3 android 5.0) again its working
有什么问题?
logcat:
D/ERROR: error => com.android.volley.TimeoutError
代码:
showLoadingDialog();
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jobj = new JSONObject(response);
JSONObject jobjHRM = jobj.getJSONObject("HRM");
int status = jobjHRM.getInt("StatusCode");
if (status == 200) {
if (jobj.getJSONObject("Data").getBoolean("result") == true) {
login_info_editor.putInt("id", jobj.getJSONObject("Data").getInt("id"));
login_info_editor.putString("username", txtlayoutid.getEditText().getText().toString());
login_info_editor.putString("password", txtlayoutpass.getEditText().getText().toString());
login_info_editor.putBoolean("loged", true);
login_info_editor.commit();
checkversion();
} else {
login_info_editor.clear();
login_info_editor.commit();
txtlayoutid.requestFocus();
Toast.makeText(getApplicationContext(), "نام کاربری یا کلمه ی عبور اشتباه است", Toast.LENGTH_LONG).show();
txtlayoutid.startAnimation(shake);
txtlayoutpass.startAnimation(shake);
hideLoadingDialog();
}
} else {
Toast.makeText(getApplicationContext(), "ارور " + status, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideLoadingDialog();
Log.d("ERROR", "error => " + error.toString());
}
}
) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(xxxxxx);
params.put(xxxxxx);
params.put(xxxxxx);
return params;
}
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
};
AppController.getInstance().addToRequestQueue(postRequest);
推荐答案
尝试配置重试策略。给出一个大的超时并尝试,
Try to configure retry policy. Give a large timeout and try,
int socketTimeout = 30000; // 30 seconds. You can change it
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postRequest.setRetryPolicy(policy);
AppController.getInstance().addToRequestQueue(postRequest);
这篇关于android volley库在蜂窝数据连接中给出超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!