且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Retrofit 收到 400 Bad request 但与邮递员一起工作

更新时间:2023-01-03 18:20:08

1.首先,你的 api 是一个 GET 方法,所以使用 @GET 代替 @POST

1.Frist thing your api is a GET Method so use @GET instead @POST

在改造中第二次尝试更改 url base url.baseUrl("https://locodealapi.herokuapp.com")到 .baseUrl("https://locodealapi.herokuapp.com/")这会起作用.或在评论中留下您的问题2.这是示例代码

Second try to change url base url in retrofit .baseUrl("https://locodealapi.herokuapp.com") to .baseUrl("https://locodealapi.herokuapp.com/") this will work. or leave your problem in comment 2.this is sample code

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(
            new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException{
                 Request original = chain.request();
                    // Request customization: add request headers
                    Request.Builder requestBuilder =original.newBuilder().
                            method(original.method(), original.body());
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }
            })
            .addInterceptor(interceptor).connectTimeout(60,TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();

    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://locodealapi.herokuapp.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient).build();
    UserApi userApi = retrofit.create(UserApi.class);
    Call<ResponseBody> call = userApi.deals("your token");
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call,retrofit2.Response<ResponseBody> response) {
        }
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {}
    });
}

@GET("api/deals")调用交易(@Header("x-access-token") String x_access_token);

@GET("api/deals") Call deals(@Header("x-access-token") String x_access_token);