在改造中动态传递标头和主体

时间:2019-01-30 11:24:12

标签: android http-headers retrofit2

我正在尝试使用改型发送HTTP标头和正文,但是出现错误。有人能帮帮我吗? 以下是我的代码: 我有一个如下的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。我不知道我做错了什么。可以请有人帮我吗?

1 个答案:

答案 0 :(得分:0)

使用方式如下,使用仅需通过的标头参数

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);