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XFJnJqTsfID9uqOwkNf46oraj9jDxic7qNSqBdunD0
在改装接口我有以下但得到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)
5条答案
按热度按时间r7s23pms1#
1.首先你的API是一个GET方法,所以使用@GET代替@POST
第二次尝试将reflect.baseUrl(“https://locodealapi.herokuapp.com“)中的url基url更改为.baseUrl(“https://locodealapi.herokuapp.com/“),这将起作用。或者把你的问题留在注解2中。这是示例代码
@GET(“API/deals”)调用deals(@Header(“x-access-token”)String x_access_token);
egmofgnx2#
今天的问候!
试试这个:
调用API如下:
这里,我假设API有一个参数,可以作为方法deals()中的第二个参数传递。可以将多个参数作为方法的实参传递。
有关详细信息,请参阅以下链接:https://futurestud.io/tutorials/retrofit-send-data-form-urlencoded
我希望这能解决你的问题。如果没有,请提供您要调用的API的完整详细信息。
bxpogfeg3#
400 response
意味着请求和服务器之间存在分歧,但是请求被创建、发送并解析了响应。这意味着Retrofit
工作正常。您可以使用OkHttp的日志拦截器记录原始请求并将其与服务器期望的内容进行比较。另外,您的
Response
大小约为5.96 MB
。这React太大了。你可以实现分页或类似的方法来分解数据,而不是在单个响应中接收这么多数据。这可能是原因之一。wlsrxk514#
在您的类中使用,它将像这样启动连接
这将是您的ApiInterface
为json打一个这样的电话
uqzxnwby5#
对我来说,问题是因为我在Gson构建器中有这样一行:
通过上面的一行,你在reflect中作为body发送的模型类中的所有字段,如果它们没有用
@Exposed
注解的话,将被忽略。删除.excludeFieldsWithoutExposeAnnotation()
解决了我的问题。