【问题标题】:Dynamically pass header and body in retrofit在改造中动态传递标题和正文
【发布时间】:2019-06-23 16:02:46
【问题描述】:

我正在尝试使用改造发送 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。我不知道我做错了什么。有人请帮帮我吗?

【问题讨论】:

  • 发布错误可能会有所帮助。
  • 您是否尝试过使用 curl、Postman、Advanced REST Client 等其他工具联系后端?换句话说,你知道它有效吗? 401 是一个身份验证/授权问题。
  • @MarkusKauppinen 是的先生,我已经尝试使用 Postman 并且没有错误

标签: android http-headers retrofit2


【解决方案1】:

如下使用,使用header参数,只需要传递

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 2017-04-15
    相关资源
    最近更新 更多