我有多线程应用程序和(我不知道为什么)我多次使用失败。在开始时,我认为问题只是网络问题。但是,如果我进入其他应用程序(如Facebook,Twitter等)需要一段时间,但请求是成功的。
这是我的BaseTask:
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;
}
}
});
}
}
有什么建议吗?
谢谢!