gpt4 book ai didi

android - 在 RoboSpice 中仅将 TLS 与 Retrofit 结合使用

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:34 24 4
gpt4 key购买 nike

我在 shell Android 应用程序中使用 Robospice 和 Retrofit 进行了基本设置,进行 REST 调用,将 JSON 响应解析为 POJO,然后我可以使用它在 Activity 中呈现。我现在只想将 TLS 用于传输安全(而不是 SSL)。我读过 Retrofit with OkHttp 可用于实现此目的,但我不知道在我的代码中的何处进行更新。

我有一个基本的界面:

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 放到应用程序中,但我不知道如何获得服务的实际实现,或者如何添加我的自定义一个以便所有请求都使用它。

有没有人有这方面的经验可以分享一些代码 fragment 或给我举个例子?

~~编辑~~

查看 Robospice 的 API,看起来我的请求可以扩展 SpiceRequest,然后在 loadFromNetwork() 方法中我只做简单的 Retrofit 和 OkHTTP 的事情。那是唯一的方法吗?认为有一种方法可以在 RetrofitSpiceService 中设置您自己的 RestAdapter 实现而不是仅仅使用默认值。

最佳答案

所以要做到这一点其实很简单。创建一个扩展 RetrofitGsonSpiceService 的类并覆盖 createRestAdapterBuilder() 方法。

例如

@Override
protected Builder createRestAdapterBuilder() {
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(SERVICE_URL)
.setRequestInterceptor(requestInterceptor);
return builder;
}

关于android - 在 RoboSpice 中仅将 TLS 与 Retrofit 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com