改造得到400不好的请求,但与邮递员合作

我的api基地的url是:

https://locodealapi.herokuapp.com/api/deals

在邮递员通过后面的标题,它工作正常。

x-access-token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ.5XFJnJqTsfID9uqOwkNf46oraj9jDxic7qNSqBdunD0 

在改造接口我有以下但得到400坏请求

 @POST("api/deals") Call<ResponseBody> deals(@Header("x-access-token") String x_access_token) 

调用代码:

 Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://locodealapi.herokuapp.com") .addConverterFactory(GsonConverterFactory.create()) .client(httpClient.build()) .build(); AppRestAPI client = retrofit.create(AppRestAPI.class); Call<ResponseBody> call1 = client.deals( token ); call1.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { Log.i(TAG, "The response is " + response.message()); Log.i(TAG, "The response is " + response.body()); try { Log.i(TAG, "The response is " + response.errorBody().string()); if (response.code() == 400) { Log.v("Error code 400",response.errorBody().string()); } } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.e(TAG, "onFailure: Failed", t); } }); 

OKHTTP日志拦截器

 11-21 21:14:30.939 3253-3342/? D/OkHttp: --> POST https://locodealapi.herokuapp.com/api/deals 11-21 21:14:30.939 3253-3342/? D/OkHttp: Content-Length: 0 11-21 21:14:30.940 3253-3342/? D/OkHttp: x-access-token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyNSwidXNlcl9uYW1lIj.... 11-21 21:14:30.942 3253-3342/? D/OkHttp: --> END POST (0-byte body) 11-21 21:14:34.188 3253-3342/? D/OkHttp: <-- 400 Bad Request https://locodealapi.herokuapp.com/api/deals (3246ms) 11-21 21:14:34.189 3253-3342/? D/OkHttp: Connection: close 11-21 21:14:34.189 3253-3342/? D/OkHttp: Server: Cowboy 11-21 21:14:34.189 3253-3342/? D/OkHttp: Date: Tue, 21 Nov 2017 15:29:33 GMT 11-21 21:14:34.190 3253-3342/? D/OkHttp: Content-Length: 0 11-21 21:14:34.191 3253-3342/? D/OkHttp: <-- END HTTP (0-byte body) 11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is Bad Request 11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is null 11-21 21:14:34.196 3253-3253/? I/BaseDrawerActivity: The response is 

注意 :相同的代码在本地nodejs服务器中工作正常(仅限http)

Solutions Collecting From Web of "改造得到400不好的请求,但与邮递员合作"

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

第二次尝试更改.baseUrl(“ https://locodealapi.herokuapp.com ”)到.baseUrl(“ https://locodealapi.herokuapp.com/ ”),这将工作。 或者在评论2中留下你的问题。这是示例代码

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

今天的问候!

尝试这个:

 @FormUrlEncoded @POST("/api/deals") Call<ResponseBody> deals(@Header("x-access-token") String x_access_token, @Field("<parameter_name>") String parameter); 

调用API如下:

  Call<ResponseBody> call = client.deals(token,"<parameter>"); 

在这里,我假设API有一个参数,可以在方法deal()中作为第二个parameter passing。 您可以传递多个参数作为方法的参数。

请参阅以下链接了解更多详情: https : //futurestud.io/tutorials/retrofit-send-data-form-urlencoded

我希望,这解决了你的问题。 如果没有,请提供您想要调用的API的完整详细信息。

一个400 response意味着这是你的请求和服务器之间的分歧,但是请求被创build,发送,并且响应被parsing。 这意味着Retrofit工作正常。 您可以使用OkHttp的日志logging拦截器来logging原始请求,并将其与服务器预期的内容进行比较。

您的Response大小约为5.96 MB 。 这个回应太大了。 而不是在一个单一的响应接收这么多的数据,你可以实现分页或类似的东西来分解数据。 这可能是其中一个原因。

 //Your Main connection Class public class ApiClient { public static final int DEFAULT_TIMEOUT_SEC = 90; public static OkHttpClient client; private static Retrofit retrofit = null; public static Retrofit getClient() { OkHttpClient.Builder httpClient = new OkHttpClient.Builder() .connectTimeout(DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS) .readTimeout(DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS) .writeTimeout(DEFAULT_TIMEOUT_SEC, TimeUnit.SECONDS); httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); System.out.println("API_KEY"+PrefManager.getActiveInstance(RecrouteCandidateApplication.getsCurrentContext()).getDeviceToken()); Request request = original.newBuilder() .header("x-access-token",YOUR_ACCESS_TOKEN"") .method(original.method(), original.body()) .build(); return chain.proceed(request); } }); //For logging the call on Logcat HttpLoggingInterceptor interceptor1 = new HttpLoggingInterceptor(); interceptor1.setLevel(HttpLoggingInterceptor.Level.BODY); httpClient.addInterceptor(interceptor1); client = httpClient.build(); if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(AppConstants.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } 

在您的类中使用,将启动这样的连接

 ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); 

这将是你的ApiInterface

 public interface ApiInterface { @POST("Your url here") Call<JsonObject> sampleMethod(); /*For non-empty body use this*/ //Call<JsonObject> getSignUpResponse(@Body JsonObject register); //For form data use this @FormUrlEncoded @POST("Your url here") Call<String> sampleMethod1(@Field(value= "param1_key", encoded = true) String param1_value); } 

为这个json打个电话

 JsonObject obj= new JsonObject(); obj.addProperty("key1","keyvalue"); Call<JsonObject> call = apiService.sample1(obj); call.enqueue(new Callback<JsonObject>() { @Override public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { if (response.body().get("status").getAsString().equals("1")) { Toast.makeText(context, response.body().get("msg").getAsString(), Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, response.body().get("msg").getAsString(), Toast.LENGTH_LONG).show(); } } @Override public void onFailure(Call<JsonObject> call, Throwable t) { Toast.makeText(context, AppConstants.NO_DATA_AVAILABLE, Toast.LENGTH_LONG).show(); } });