2

I have common request and response with AsyncTask.THREAD_POOL_EXECUTOR. I have manually implemented retry count. Now what happening if two web service working simultaneously then its using retry count of each other.

I am using retrofit with client OkHttpClient.

Dhrupal
  • 1,863
  • 1
  • 23
  • 38
  • I think you should use Synchronized Method and inside that method you should pass that counter and increase the value. – Gokul Aug 16 '16 at 12:52

2 Answers2

0

I would suggest you to switch to rxandroid and take advantage of retry()

Marco C.
  • 1,282
  • 2
  • 15
  • 19
0

I solved it using custom call back (overriding Callback of retrofit). In this class pass retry count. So on response success or failure you will have access to variable retry count which will be web service specific. So will not interfere each other in case of parallel execution.

@POST("/xyz/abc/")
    void getSomeData(
            @Body TypedInput body,
            Callback<Response> callback);

You can create and pass your custom callback and save some object/int value to uniquely identify request.

public interface CustomOnRequestFinishedListener {

    void onSuccess(Object response, Response retrofitResponse, Object data);

    void onError(RetrofitError error, Object data);
}

Implement CustomOnRequestFinishedListener where you want to call CustomCallback and will get success/failure callback

public class CustomCallback extends CallBack
    {
    private final Object data;
private final CustomOnRequestFinishedListener customOnRequestFinishedListener;
    public CustomCallback(CustomOnRequestFinishedListener customOnRequestFinishedListener, Object data)
    {
        this.data = data;
this.customOnRequestFinishedListener = customOnRequestFinishedListener;
    }

    @Override
    public void success(Object o, Response response)
    {
        customOnRequestFinishedListener.onSuccess(o, response, data);
    }

    @Override
    public void failure(RetrofitError error)
    {
        customOnRequestFinishedListener.onError(error, data);
    }
    }

By this way you will initially setting some unique data in request and will get same data in response.

Ramit
  • 416
  • 3
  • 8
  • How will I set Listener in interceptor. Need to create a object of interceptor explicitly right? – Dhrupal Aug 22 '16 at 06:05
  • Not in interceptor. In api call you can provide your custom listener for particular api. Editing my ans... – Ramit Aug 22 '16 at 06:07
  • CustomCallback extends CallBack or CustomCallback implements Callback? – Dhrupal Aug 22 '16 at 06:19
  • extends as mentioned in answer. – Ramit Aug 22 '16 at 06:27
  • Are you talking to extends retrofit2.Callback? – Dhrupal Aug 22 '16 at 06:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121506/discussion-between-ramit-and-dhrupal). – Ramit Aug 22 '16 at 06:37
  • Ok fine, I am using old version but you can change it Use onResponse(Call call, Response response) in place of success and use onFailure(Call call, Throwable t) in place of failure – Ramit Aug 22 '16 at 10:37