1

マルチスレッド アプリケーションを使用していますが、(理由はわかりません) onFailure が複数回発生しています。当初、問題はネットワークだけだと思います。ただし、他のアプリケーション (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;
                        }
                    }
                });

    }
}

なにか提案を?

ありがとう!

4

0 に答える 0