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XFJnJqTsfID9uqOwkNf46oraj9jDxic7qNSqBdunD0
在改造界面中,我有以下内容,但得到400 Bad request
@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)
中正常工作答案 0 :(得分:2)
1.Frist的事你的api是一个GET方法,所以使用@GET而不是@POST
第二次尝试在改造时更改网址基本网址 .baseUrl(&#34; https://locodealapi.herokuapp.com&#34) 到.baseUrl(&#34; https://locodealapi.herokuapp.com/&#34;) 这会奏效。或者将你的问题留在评论中 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(&#34; API /优惠&#34) 调用交易(@Header(&#34; x-access-token&#34;)字符串x_access_token);
答案 1 :(得分:1)
当天的问候!
试试这个:
@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有一个参数,可以在方法deals()中作为第二个参数传递。您可以传递多个参数作为方法的参数。
有关详细信息,请参阅以下链接: https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded
我希望,这可以解决你的问题。如果没有,请提供您要呼叫的API的完整详细信息。
答案 2 :(得分:0)
400 response
表示您的请求与服务器之间存在分歧,但请求已创建,已发送,并且已解析响应。这意味着Retrofit
工作正常。您可以使用OkHttp的日志记录拦截器来记录原始请求,并将其与服务器期望的内容进行比较。
此外,Response
尺寸大约为5.96 MB
。这是太大的反应。您可以实现分页或类似于分解数据的内容,而不是在单个响应中接收这么多数据。这可能是其中一个原因。
答案 3 :(得分:0)
//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();
}
});
答案 4 :(得分:0)
对我来说,问题是因为我在Gson生成器中有以下一行:
.excludeFieldsWithoutExposeAnnotation()
在上面的行中,如果未使用@Exposed
注释对您作为主体发送的模型类中的所有字段,则将忽略它们。
删除.excludeFieldsWithoutExposeAnnotation()
为我解决了这个问题。