android - 改造 2 中永远不会发送标头参数
问题描述
在我的 Android 应用程序中,我正在尝试使用改造 2 进行网络调用,并且我需要在标头中发送一些数据。我尝试使用@Headers 静态发送它们,使用@Header 和@HeaderMap 动态发送它们,但它们都不起作用。我还尝试将标头添加到 okhttpclient 拦截器,但仍然无法正常工作。可能是什么问题以及如何解决?
下面是我的改造代码
public class ApiClient {
public static final String BASE_URL =
"https://iconprojectss.com/2021/Tam/webServices/";
private static ApiClient apiClient;
private Retrofit retrofit;
private ApiClient(){
Gson gson = new GsonBuilder()
.create();
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("lang", "en")
.header("apiKey", "fd94f23499b954d6cea823567a606a3f")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
public static synchronized ApiClient getApiClient(){
if (apiClient == null){
apiClient = new ApiClient();
}
return apiClient;
}
public ApiInterface getApiInterface(){
return retrofit.create(ApiInterface.class);
}
}
和我的界面
@Headers({
"lang: en",
"apiKey: fd94f23499b954d6cea823567a606a3f"
})
@POST("login/")
Call<ApiResponse> login(
@Body HashMap<String, Object> body);
@POST("login/")
Call<ApiResponse> login(@HeaderMap Map<String, String> headers,
@Body HashMap<String, Object> body);
@POST("login/")
Call<ApiResponse> login(@Header("lang") String lang,
@Header("apiKey") String apiKey
@Body HashMap<String, Object> body);
}
在前面的代码中,我尝试了所有可能的方法来在界面中添加标题
最后这是我拨打电话的代码
public void loginRequest(String emailStr, String passwordStr, LoginCallbacks loginCallbacks) {
Map<String, String> hed = new HashMap<>();
hed.put("lang", "en");
hed.put("apiKey", "fd94f23499b954d6cea823567a606a3f");
this.loginCallbacks = loginCallbacks;
HashMap<String, Object> map = new HashMap<>();
map.put("email", emailStr);
map.put("password", passwordStr);
map.put("fcmToken", FCM_TOKEN);
ApiClient.getApiClient().getApiInterface().login(
map)
.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
ApiResponse result = response.body();
if (result != null) {
int code = result.getCode();
String message = result.getMsg();
if (code == 200) {
loginCallbacks.onSuccess(message);
} else {
loginCallbacks.onFail(message);
}
} else {
loginCallbacks.onFail("zero");
}
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
loginCallbacks.onFail(t.getMessage());
}
});
}
请注意,我正在使用的改装版本是implementation 'com.squareup.retrofit2:retrofit:2.9.0'
解决方案
推荐阅读
- python-3.x - Scipy Optimize Basin Hopping 失败
- sql - 在多值参数上选择“全选”时如何做出不同的行为
- django - 使用第三方登录系统时如何检查用户是否登录?
- winforms - 微软报表查看器表达式求值问题
- android - 如何使用 Android 分页库向 PagedList 添加标题
- typescript - 如何在 Typescript 中访问匿名对象中的类参数?
- javascript - 反应原生在状态数组中添加新项目
- c++ - 如何使用继承Threadsafe制作QString
- keras - Keras 网络处理两个输入图像
- angular - Angular router.navigate 未重定向到目标页面