Retrofit generic reponse handler

2019-07-09 01:42发布

I wish to handle all my responses in single method. the purpose is to recall the service when the response code is not 3, when the response code is 3 I intend to first refresh the token and then recall the same service.

I've created a Basecallback class to catch one method but I can't see the log and can't catch breakpoints.

BASECALLBACK.class

public class BaseCallBack<T> implements Callback<T> {

 @Override
 public void onResponse(Call<T> call, Response<T> response) {

     if (!response.isSuccessful()){
         Log.d("BaseCallback","Response Fail");
     }

 }

 @Override
 public void onFailure(Call<T> call, Throwable t) {
     t.toString();
 }
}

CALL METHOD

 ApiManager.getInstance().getUsers().enqueue(new BaseCallBack<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {

                if (response.isSuccessful()){

                }

            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {

            }
        });

I just want to handle my services single method. Appreciate any suggestion or help.

1条回答
男人必须洒脱
2楼-- · 2019-07-09 02:09

Your starting point is good - you have an ApiManager which is the single point you're looking for - a class, NOT a method (methods shouldn't be a single contact point in this case, it will make your code unreadable and harder to debug later.

From here it would probably be better to use your own custom interface, and implement it however you wish from where you call the request, there you can handle the stuff you want, this is a very generic example that should fix some stuff and get you going.

Be mindful to the fact that this still requires you to work - tweak and add the stuff you need.

This is all you need as an interface (very basic, you can add stuff)

public interface CustomCallListener<T>
{
 public void getResult(T object); 
}

This is how you should use it in you ApiManager - it receives your interface as a parameter carrying the expected object type, when the response returns do what you need - parse it, cut it, whatever - and cast it into the correct object, this example uses a String response and a List return object, you can expect whatever you think and parse it accordingly, Retrofit2 allows you to parse JSON strings directly (using GSON or some other library), so it's your decision on what to use here - if you don't know what I mean - read about it.

This is also where I would add breakpoints and Log. calls to debug the response you get as you get it you can also break down rawResponse for headers and other stuff.

class ApiManager
{
 // .. other stuff you need...

public void getSomeList(final CustomCallListener<List<SomeObject>> listener)
{
    Call<ResponseBody> request = serviceCaller.getSomeInfo(userid);

    //...other stuff you might need...

    request.enqueue(new Callback<ResponseBody>()
    {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
        {
            try
            {
                String response = rawResponse.body().string();
                //...other stuff you might need...
                //...do something with the response, convert it to 
                //return the correct object...
                SomeObject object = new SomeObject(response);
                listener.getResult(object);

            }
            catch (Exception e)
            {
            // .. the response was no good...
                listener.getResult(null);
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable throwable)
        {
          // .. the response was no good...
            listener.getResult(null);
        }
    });
 }
}

Finally this is what you should use from anywhere in your code - this allows you to implement the callback right there and handle anything you need by what you return from the ApiManager.

  ApiManager.getInstance().getSomeList(new CustomCallListener<List<SomeObject>>()
    {
        @Override
        public void getResult(List<SomeObject> objects)
        {
            if (null != objects)
            {

                 // check objects and do whatever...
            }
            else
            {
             // ... do other stuff .. handle failure codes etc.
            }
        }
    });

Stuff to notice

As mentioned - this is a very generic skeleton that can be greatly modified (add more methods to the interface with different return types to handle Exceptions, Bad responses, other objects, add more params to the same method to handle more options, etc.) read about the subject more, beware of passing null Objects, use try and catches to avoid crashes.

Hope this Helps!

查看更多
登录 后发表回答