我有多线程应用程序并且(我不知道为什么)我多次遇到 onFailure。一开始我认为问题只是网络。但是,如果我进入其他应用程序(如 Facebook、Twitter 等)需要一段时间,但请求成功。
这是我的基本任务:
public abstract class TaskPadrao {
protected Context context;
protected Exception exception;
protected Gson gson;
protected MyApplication application;
protected String response;
protected String errorResponse;
protected static AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
final int DEFAULT_TIMEOUT = 60 * 1000;
protected TaskPadrao() {
asyncHttpClient.setTimeout(DEFAULT_TIMEOUT);
configSSL();
configuraHeader();
}
private void configSSL() {
//configuring SSL
}
private void configuraHeader() {
//configuring Header auth app
}
public TaskPadrao(Context context) {
this.context = context;
this.gson = new Gson();
this.application = (MyApplication) context.getApplicationContext();
configSSL();
configuraHeader();
}
}
这是我的应用程序中每 5 秒运行一次的类:
public class VerificaStatusTask extends TaskPadrao {
public VerificaStatusTask(Context context) {
super(context);
}
int tryAgain = 5;
public void callStatus(final long idCall) {
CallTo CallTo = new CallTo();
CallTo.setIdChamada(idChamada);
Request<CallTo> Request = new Request<CallTo>(application.getSesseionId());
Request.setMetodo("METHOD");
Request.setPost(CallTo);
RequestParams params = new RequestParams();
params.put("data", gson.toJson(Request));
asyncHttpClient.post("url_server", params,new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject arg0) {
try {
//stuffs with return
} catch (JSONException e) {
e.printStackTrace();
//trying again
callStatus(idCall);
}
}
@Override
public void onFailure(Throwable arg0) {
if(tryAgain != 0){
callStatus(idCall);
tryAgain --;
}else{
//post again in 5 seconds
((MyActivity) context).postAgain();
tryAgain = 5;
}
}
});
}
}
有什么建议吗?
谢谢!