我已經使用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
實現,而不是僅使用默認設置。