Android N秒后发出截击请求
我正在使用截击库。我有一个问题。我无法在N秒(30秒)后停止请求 这是来源Android N秒后发出截击请求,android,http,android-volley,androidhttpclient,Android,Http,Android Volley,Androidhttpclient,我正在使用截击库。我有一个问题。我无法在N秒(30秒)后停止请求 这是来源 private void searchTrains() { RequestQueue queue = Volley.newRequestQueue(getActivity()); GlobalParameters.pDialog = new TrainCustomLoader(getActivity()); GlobalParameters.pDialog.show(); GlobalPa
private void searchTrains() {
RequestQueue queue = Volley.newRequestQueue(getActivity());
GlobalParameters.pDialog = new TrainCustomLoader(getActivity());
GlobalParameters.pDialog.show();
GlobalParameters.pDialog.setCancelable(false);
String url = TrainUrls.trains();
UTF8VolleySupport sr = new UTF8VolleySupport(Request.Method.GET, url, new Response.Listener<String>() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onResponse(final String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
if (GlobalParameters.pDialog != null)
GlobalParameters.pDialog.dismiss();
NetworkResponse errorRes = error.networkResponse;
String stringData = "";
int statusCode = errorRes.statusCode;
if (errorRes != null && errorRes.data != null) {
try {
stringData = new String(errorRes.data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Log.e("Error", stringData);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Accept", "application/json");
params.put("Authorization", "Bearer" + " " + token);
return params;
}
};
sr.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(sr);
}
private void searchTrains(){
RequestQueue=Volley.newRequestQueue(getActivity());
GlobalParameters.pDialog=新的TrainCustomLoader(getActivity());
GlobalParameters.pDialog.show();
全局参数.pDialog.setCancelable(false);
字符串url=trainURL.trains();
UTF8VolleySupport sr=新的UTF8VolleySupport(Request.Method.GET,url,new Response.Listener()){
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
公共void onResponse(最终字符串响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
if(GlobalParameters.pDialog!=null)
GlobalParameters.pDialog.disclose();
NetworkResponse errorRes=错误。NetworkResponse;
字符串stringData=“”;
int statusCode=errorRes.statusCode;
if(errorRes!=null&&errorRes.data!=null){
试一试{
stringData=新字符串(errorRes.data,“UTF-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}
Log.e(“错误”,stringData);
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“接受”、“应用程序/json”);
参数put(“授权”、“承载人”+“”+令牌);
返回参数;
}
};
高级setRetryPolicy(新的DefaultRetryPolicy)(
30000,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
添加(sr);
}
正如我所说,我可以从服务器得到响应,但我不能在30秒后停止请求
如何解决我的问题?您是否尝试过将默认最大重试次数设置为零或一次?我尝试了零次,结果成功了。谢谢。我不知道为什么截击使用默认值1我怎么能做到?我是新用户..@SushobhNadiger Nadiger