实施Retrofit2服务接口 [英] Implementing Retrofit2 service interface

查看:83
本文介绍了实施Retrofit2服务接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下以下情况.

我将Retrofit用作我的API客户端,并且我将使用缓存机制将某些响应持久化在本地(比如说SQLite数据库)中,因此某些数据仅获得一次.为以下接口创建自定义实现似乎是一个完美的案例,当网络不可用时,该接口将用于从本地数据库中获取数据.

I am using Retrofit as my API client and I am going to use a caching mechanism to persist some responses locally in let's say SQLite database, so some of the data is obtained only once. It seems like a perfect case to create a custom implementation for the following interface which is going to fetch the data from local database when network is not available.

public interface CommentsService {  
    @GET("posts/{postId}/comments")
    Call<List<Comment>> getCommentsOfPost(@Path("postId") int id);
}

问题在于Retrofit2中的所有服务方法都应包装在 Call 对象中,因为它像以前版本的Retrofit一样防止将回调作为参数传递.

The problem is that all service methods in retrofit2 should be wrapped in a Call object, since it prevents passing the callback as a parameter like in previous versions of retrofit.

我想知道创建此接口的自定义实现是否是一种好习惯,如果是,那么如何处理Call对象?

I would like to know if it is a good practice to create a custom implementation of this interface, if yes then how to treat with the Call object ?

推荐答案

在我的一个项目中,我遇到了类似的问题,无法使用RxJava.我通过使用命令模式解决了这个问题.

In one of my projects I had a similar problem and could not use RxJava. I solved this by using a Command Pattern.

首先,我创建了一个带有清晰API的自定义 Callback 类.这样,我可以更简洁地处理HTTP代码(例如400或500).

First, I have created a custom Callback class with a clear API. That way I can handle HTTP codes more concisely (such as 400 or 500).

public abstract class RestCallback<T> implements Callback<T> {

    @Override
    public void onResponse(Response<T> response, Retrofit retrofit) {
        if (response != null) {
            if (response.isSuccess()) {
                onSuccess(response, retrofit);
            } else {
                onError(response, retrofit);
            }
        } else {
            onFailure(null);
        }
    }

    abstract public void onSuccess(Response<T> response, Retrofit retrofit);

    abstract public void onError(Response<T> response, Retrofit retrofit);

    abstract public void onFailure(Throwable t);

}

然后我创建了一个 Command 界面:

Then I created a Command interface:

public interface Command {

    // New network hit
    void fresh(RestCallback callback);

    // First cached if available then fresh
    void all(RestCallback callback);

    // Cached response only
    void cached(RestCallback callback);

    // If cache exists return it, otherwise return a fresh response
    void get(RestCallback callback);

    // Cancel the request
    void cancel();

}

这样,将直接实现Command接口并与其一起使用,而不是直接与Retrofit对象一起使用.例如,您可以创建一个GetCommentsOfPostCommand并让它处理Retrofit Call对象,并封装请求逻辑.

This way, instead of working directly with Retrofit objects you will implement the Command interface and work with that. For example, you could create a GetCommentsOfPostCommand and let it take care of the Retrofit Call object, encapsulating request logic.

通过这种方式,您可以随心所欲地工作;在公开定义明确的接口的同时实现缓存,请求,取消等.

This way you can work as you like; implementing cache, requests, cancellations, etc. while exposing a well-defined interface.

希望这会有所帮助.

这篇关于实施Retrofit2服务接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆