gpt4 book ai didi

android - Retrofit2 单例实例

转载 作者:行者123 更新时间:2023-11-30 01:15:53 25 4
gpt4 key购买 nike

我正在尝试为 Retrofit2 创建单例实例,它工作正常。但是一旦我想要动态 header ,我就无法这样做。

public class ApiManager {


public final static String BASE_URL = "URL";



private static ApiManager instance =null;
private ApiModule apiModule;

public interface ApiModule {


@GET("exists")
Call<ServerStatus> checkExistsTeamName(@Path("teamName") String teamName);


}


private ApiManager(){
final TimeZone tz = TimeZone.getDefault();
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
try {
client.interceptors().add(new Interceptor() {
@Override
public com.squareup.okhttp.Response intercept(Interceptor.Chain chain) throws
IOException {

Request original = chain.request();
Request request = original.newBuilder()
.header("X-API-Version", "1")
.header("X-USER-TIMEZONE", tz.getID())
.method(original.method(), original.body())
.build();


return chain.proceed(request);

}
});
}catch (Exception e){

}
client.interceptors().add(interceptor);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();

apiModule = retrofit.create(ApiModule.class);
}





public static ApiManager getInstance() {
if(instance == null) {
instance = new ApiManager();
}
return instance;
}




public ApiModule getService() {
return apiModule;
}

public ApiModule getService(String token){
return apiModule;
}




}

在其他 Activity 中,我可以接到电话进行改造。

ApiManager apiManager = ApiManager.getInstance();apiManager.getService().checkExistsTeamName("参数")

这在这里工作正常但是如果我想添加额外的额外动态 header 我应该怎么做?我被困在这里

最佳答案

你需要某种依赖注入(inject)。试试这段代码。在你调用你的服务之前,打电话

ApiManager.setHeaders(map of headers);

带有 header 值。使用空映射或 null 调用以排除它们。

public class ApiManager {
public final static String BASE_URL = "URL";
private static ApiManager instance =null;
private ApiModule apiModule;

public interface ApiModule {
@GET("exists")
Call<ServerStatus> checkExistsTeamName(@Path("teamName") String
teamName);
}

private static Map<String, String> headers = new HashMap<>();

public static void setHeaders(Map<String, String> headers) {
ApiManager.headers = headers;
}

private ApiManager(){
final TimeZone tz = TimeZone.getDefault();
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
try {
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws
IOException {

Request original = chain.request();
Request.Builder builder = original.newBuilder()
.header("X-API-Version", "1")
.header("X-USER-TIMEZONE", tz.getID())
.method(original.method(), original.body());

if(headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}

Request request = builder.build();

return chain.proceed(request);

}
});
}catch (Exception e){

}
client.interceptors().add(interceptor);

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();

apiModule = retrofit.create(ApiModule.class);
}





public static ApiManager getInstance() {
if(instance == null) {
instance = new ApiManager();
}
return instance;
}




public ApiModule getService() {
return apiModule;
}

public ApiModule getService(String token){
return apiModule;
}


}

关于android - Retrofit2 单例实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37865665/

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