我已经使用Robospice进行了基本设置,并在使用REST调用的shell Android应用程序中进行了Retrofit,将JSON响应解析为POJO,然后可以在Activity中呈现。我现在想仅将TLS用于传输安全性(不是SSL)。我已经读过OkHttp的Retrofit可以用来实现这一点,但我不知道我的代码中更新的位置。仅在RoboSpice中使用TLS进行改造
我有一个基本的接口:
public interface RandomAPI {
@GET("/users")
List<User> getUsers(@Path("owner") String owner, @Path("repo") String repo);
@GET("https://stackoverflow.com/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放入应用程序中,但我不知道如何获得服务的实际实现,或者如何添加自定义的服务以便所有请求都使用它。
有没有人有这方面的经验,可以分享一些代码片段或指向我的例子?
~~编辑~~
展望为Robospice的API,看起来像我的要求可以只延长SpiceRequest,然后内loadFromNetwork()方法,我只是做简单的改造和OkHTTP东西。那是唯一的方法吗?认为有一种方法可以在RetrofitSpiceService
中设置自己的RestAdapter
实现,而不是仅使用默认设置。