【发布时间】:2015-06-30 23:16:04
【问题描述】:
我在一个外壳 Android 应用程序中使用带有 Retrofit 的 Robospice 进行了基本设置,进行 REST 调用,将 JSON 响应解析为 POJO,然后我可以使用它在 Activity 中呈现。我现在只想将 TLS 用于传输安全(而不是 SSL)。我已经读过可以使用带有 OkHttp 的 Retrofit 来实现这一点,但我不知道在我的代码中在哪里进行更新。
我有一个基本界面:
public interface RandomAPI {
@GET("/users")
List<User> getUsers(@Path("owner") String owner, @Path("repo") String repo);
@GET("/users/{userid}")
User getUser(@Path("userid") int userID);
}
我有一个服务:
public class RandomService extends RetrofitGsonSpiceService {
private final static String BASE_URL = "http://jsonplaceholder.typicode.com";
@Override
public void onCreate() {
super.onCreate();
addRetrofitInterface(RandomAPI.class);
}
@Override
protected String getServerUrl() {
return BASE_URL;
}
}
最后是一个请求:
public class RandomRequest extends RetrofitSpiceRequest<User, RandomAPI> {
private int userID;
public RandomRequest(int userID) {
super(User.class, RandomAPI.class);
this.userID = userID;
}
@Override
public User loadDataFromNetwork() throws Exception {
return getService().getUser(userID);
}
}
我猜我需要更新服务,但不确定如何更新。我真的很喜欢这种模式的简单性,所以如果可能的话,我想保留它。我可以将 OkHttp jar 放到应用程序中,但我不知道如何获得服务的实际实现,或者如何添加我的自定义一个以便所有请求都使用它。
有没有人有这方面的经验可以分享一些代码sn-ps或给我一个例子?
~~编辑~~
查看 Robospice 的 API,看起来我的请求可以扩展 SpiceRequest,然后在 loadFromNetwork() 方法中我只做简单的 Retrofit 和 OkHTTP 的东西。这是唯一的方法吗?认为有一种方法可以在 RetrofitSpiceService 中设置您自己的 RestAdapter 实现,而不仅仅是使用默认值。
【问题讨论】:
标签: android ssl retrofit okhttp robospice