Building a library that uses Retrofit internally,

2019-06-03 00:12发布

问题:

I'm trying to build a library that basically wraps our api. Basically, the structure im going for is something like this:

MySDK mySDK = new MySDK("username", "password");

mySDK.getPlaylistInfo("3423", 2323, new CustomCallback<>(){

//on response
//on failure

});

So with vanilla Retrofit, an api call usually looks something like the following:

ApiService api = retrofit.create(ApiService.class);
Call<Response> call = api.getPlaylistInfo()
call.enqueue(new Callback<Response>() {
    @Override
    public void onResponse(Call<Response> call, Response<Response> response) {
        //handle response
    }

    @Override
    public void onFailure(Call<Response> call, Throwable t) {
        //handle failure
    }

});

Basically, how would I wrap retrofits callback system into my own? Note, the reason for needing to do this is to preprocess the data returned from the api before delivering the final response.

回答1:

I've written something similar so it might help you getting started, this follows an implementation I'v written for Volley, and re-used when I migrated to Retrofit2 so it resembles it (this SO question).

Create a global object (what you would refer to as MySDK) as a singelton class that handles your requests:

create a singleton class, which you instatiate when you're application comes up:

public class NetworkManager
{
    private static final String TAG = "NetworkManager";
    private static NetworkManager instance = null;

    private static final String prefixURL = "http://some/url/prefix/";

    //for Retrofit API
    private Retrofit retrofit;
    private ServicesApi serviceCaller;

    private NetworkManager(Context context)
    {
        retrofit = new Retrofit.Builder().baseUrl(prefixURL).build();
        serviceCaller = retrofit.create(ServicesApi.class);
        //other stuf if you need
    }

    public static synchronized NetworkManager getInstance(Context context)
    {
        if (null == instance)
            instance = new NetworkManager(context);
        return instance;
    }

    //this is so you don't need to pass context each time
    public static synchronized NetworkManager getInstance()
    {
        if (null == instance)
        {
            throw new IllegalStateException(NetworkManager.class.getSimpleName() +
                    " is not initialized, call getInstance(...) first");
        }
        return instance;
    }

    public void somePostRequestReturningString(Object param1, final SomeCustomListener<String> listener)
    {
        String url = prefixURL + "this/request/suffix";

        Map<String, Object> jsonParams = new HashMap<>();
        jsonParams.put("param1", param1);

        Call<ResponseBody> response;
        RequestBody body;

        body = RequestBody.create(okhttp3.MediaType.parse(JSON_UTF), (new JSONObject(jsonParams)).toString());
        response = serviceCaller.thePostMethodYouWant("someUrlSufix", body);

        response.enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
            {
                try
                {
                  String response = rawResponse.body().string();

                  // do what you want with it and based on that...
                  //return it to who called this method
                  listener.getResult("someResultString");
                }
                catch (Exception e)
                {
                 e.printStackTrace();
                 listener.getResult("Error1...");
                }
            }  
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable)
            {
                try
                {
                 // do something else in case of an error
                 listener.getResult("Error2...");
                }
                catch (Exception e)
                {
                 throwable.printStackTrace();
                 listener.getResult("Error3...");
                }
            }
        });
    }

    public void someGetRequestReturningString(Object param1, final SomeCustomListener<String> listener)
    {
        // you need it all to be strings, lets say id is an int and name is a string
        Call<ResponseBody> response = serviceCaller.theGetMethodYouWant
            (String.valueOf(param1.getUserId()), param1.getUserName());

        response.enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
            {
                try
                {
                  String response = rawResponse.body().string();

                  // do what you want with it and based on that...
                  //return it to who called this method
                  listener.getResult("someResultString");
                }
                catch (Exception e)
                {
                 e.printStackTrace();
                 listener.getResult("Error1...");
                }
            }  
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable)
            {
                try
                {
                // do something else in case of an error
                 listener.getResult("Error2...");
                }
                catch (Exception e)
                {
                 throwable.printStackTrace();
                 listener.getResult("Error3...");
                }
            }
        });
    }
}

This works with your interface (example with POST and GET request, GET could be without params):

 public interface BelongServicesApi
 {
    @POST("rest/of/suffix/{lastpart}") // with dynamic suffix example
    Call<ResponseBody> thePostMethodYouWant(@Path("lastpart") String suffix, @Body RequestBody params);

    @GET("rest/of/suffix") // with a fixed suffix example
    Call<ResponseBody> theGetMethodYouWant(@Query("userid") String userid, @Query("username") String username);
 }

when your application comes up:

public class MyApplication extends Application
{
  //...

    @Override
    public void onCreate()
    {
        super.onCreate();
        NetworkManager.getInstance(this);
    }

 //...

}

a simple listener interface for your callback (seperate file would do good):

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

and finally, from wherever you want, the context is already in there, just call:

public class BlaBla
{
    //.....

        public void someMethod()
        {
            //use the POST or GET 
            NetworkManager.getInstance().somePostRequestReturningString(someObject, new SomeCustomListener<String>()
            {
                @Override
                public void getResult(String result)
                {
                    if (!result.isEmpty())
                    {
                     //do what you need with the result...
                    }
                }
            });
        }
}

you can use any object with the listener, just parse the response string to a corresponding object, depending on what you need to receive and you can call that from everywhere (onClicks, etc.), just remember the objects need to match between methods.

Hope this Helps!