android - 在改造中动态传递 header 和主体

标签 android http-headers retrofit2

我正在尝试使用改造发送http header 和正文,但出现错误。有人能帮帮我吗? 以下是我的代码: 我有一个 ApiClient 如下:

public class ApiClient {
public static Retrofit retrofit = null;
public static Retrofit getApiClient(final String token) {

    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("Authorization", "Bearer "+token)

                                              .method(original.method(), original.body())
                                              .build();

                                      return chain.proceed(request);
                                  }
                              });
    OkHttpClient client = httpClient.build();






    if (retrofit == null) {
        retrofit = new Retrofit.Builder().baseUrl(BaseUrl.BASE_URL)
                    .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

以下是我的界面:

public interface RegisterInterface {
  @POST("api/userData/registerNewUserData")
Call<RegisterModel> getData(@Body RequestBody body);

}

下面是我需要使用 api 的类:

registerInterface = ApiClient.getApiClient(accessToken).create(RegisterInterface.class);
                Call<RegisterModel> call = registerInterface.getData(requestBody);
                call.enqueue(new Callback<RegisterModel>() {
                    @Override
                    public void onResponse(Call<RegisterModel> call, Response<RegisterModel> response) {
                        Log.e("code", response.code() + "");

                        String message = response.body().getMessage();
                        if (message.equals("success")) {
                            Toast.makeText(RegisterClass.this, "Data Sent Successfully", Toast.LENGTH_SHORT).show();
                            finish();
                            startActivity(getIntent());
                        }
                    }

                    @Override
                    public void onFailure(Call<RegisterModel> call, Throwable t) {

                    }
                });
            }
            else {
                Toast.makeText(RegisterClass.this, "Please fill all the fields", Toast.LENGTH_SHORT).show();
            }
        }
    });

状态码是401。我不知道我做错了什么。有人请帮助我吗?

最佳答案

使用方式如下,只使用需要传递的头参数

Map<String, String> stringStringMap = new HashMap<>();
        stringStringMap.put("Authorization", "Bearer "+token));
        stringStringMap.put("Content-Type", "application/json");
        stringStringMap.put("Accept", "application/json");

@POST("api/userData/registerNewUserData")
Call<RegisterModel> getData(@HeaderMap Map<String, String> headers,@Body RequestBody body);

关于android - 在改造中动态传递 header 和主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54439482/

相关文章:

android - 使用 Retrofit 2.0 Android 进行多部分请求 为什么我看不到里面的多部分?

java - Spring Boot 服务器在使用 Retrofit2 进行 POST 时出错,仅当数据库中存在关系时才会发生

android - 提高识别率的图像预处理步骤

android - Google Play - 完全阴影 apk

apache-flex - 使用 Flex RemoteObject 方法时如何设置 HTTP header ?

node.js - 限制 CORS 来源,Node/Express 不起作用

android - Retrofit 返回空数组列表,但给出了有效的 POJO 和 Json 来处理(尝试了其他问题,但对它们起作用的答案不在 mne 上)

android - 导出 C 局部变量 : JNI run-time behavior issue

android - 使用 android maps api v2,如何跟踪用户位置(类似于 google maps api 跟踪方式)。用户位置标记始终保持在中心

http - 如何判断用户是否通过我的免费域名访问了我的网站