com*_*you 12 api android android-volley
public void getTestDats(String unique_id) {
final String tag = "testList";
String url = Constants.BASE_URL + "test_module.php";
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", SharedPreferenceUtil.getString(Constants.PrefKeys.PREF_USER_ID, "1"));
params.put("unique_id", unique_id);//1,2,3,4,5
DataRequest loginRequest = new DataRequest(Method.POST, url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
switch (response.optInt("unique_id")) {
case 1:
//task 1
break;
case 2:
//task 2
break;
default:
//nothing
}
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//I want to know which unique_id request is failed
}
});
loginRequest.setRetryPolicy(new DefaultRetryPolicy(20000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(loginRequest, tag);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用unique_id识别哪个请求失败.
我正在使用unique_id调用getTestDats("1")函数.函数调用10次,所有api调用addToRequestQueue.
当API进入Success部分时,它按照代码工作.但是当API进入错误部分时,我没有识别请求.有没有办法知道我的请求参数,所以我可以重试特定的unique_id请求.
设置一个字段 inloginRequest和 inonErrorResponse访问该字段,例如loginRequest.getUniqueId()
或者,创建一个单独的类来实现 Response.Listener 和 ErrorListener
响应监听器类:
public class MyReponseListener implements Response.Listener<JSONOBject>{
private long uniqId;
public MyResponseListener(long uniqId){
this.uniqId = uniqId;
}
@Override
public void onResponse(JSONObject response) {
System.out.println("response for uniqId " + uniqId);
// do your other chit chat
}
}
Run Code Online (Sandbox Code Playgroud)
错误监听器类:
public class MyErrorListener implements ErrorListener{
private long uniqId;
public MyErrorListener(long uniqId){
this.uniqId = uniqId;
}
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error for uniqId : " + uniqId);
}
}
Run Code Online (Sandbox Code Playgroud)
现在这样称呼它:
DataRequest loginRequest = new DataRequest(Method.POST, url, params, new MyResponeListener(uniqId), new MyErrorListener(uniqId));
Run Code Online (Sandbox Code Playgroud)
现在,如果您希望在 ErrorListener 类中可以访问调用类的某些代码,请执行以下操作: 1. 在调用类中,将您想要访问的代码放入方法中 2. 使用这些方法创建一个接口 3. 调用类将实现该接口 4. 将接口传递给 MyErrorListener 或 MyResponseListener 的构造函数
例如,一个活动调用齐射请求,出现错误时您想要显示一条消息。将显示错误代码放在方法中:
public void showMessage(int errorCode){
//message according to code
}
Run Code Online (Sandbox Code Playgroud)
现在创建一个界面
public interface errorMessageInterface{
void showMessage(int errorCode);
}
Run Code Online (Sandbox Code Playgroud)
将activity实现errorMessageInterface并将其传递给 的构造函数MyErrorListener并将其保存在field.
在里面onErrorResponse,你会打电话
field.showMessage()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |