我有一个关于 android 应用程序的 API,它工作了很长时间。系统得到更新,现在我在同一个请求中收到“400 - Bad Request”(没有更改)。但在 Postman 或 Advanced REST Client 中运行良好。
我正在使用:Java Android Retrofit 2 OkHTTP GSON
安卓应用
接口登录服务
@Headers({
"Content-Type: application/json",
"Accept: application/json",
"Content-Length: 61"
})
@POST(Constants.API_LOGIN)
Call<Login> login(@Body JsonObject json);
登录
JsonObject json = new JsonObject();
json.addProperty("email", AUTH_USER);
json.addProperty("password", AUTH_PASS);
Call<Login> call = new RetrofitConfig().getLoginService().login(json);
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(@NonNull Call<Login> call, @NonNull Response<Login> response) {
Login login = response.body();
Log.d(TAG, "RESPONSE: " + response.message());
String msg = "Autenticado com sucesso.";
}
@Override
public void onFailure(@NonNull Call<Login> call, @NonNull Throwable t) {
Log.e(TAG, "Error: " + t.getMessage(), t);
Toast.makeText(context, "Erro ao autenticar com servidor", Toast.LENGTH_SHORT).show();
call.cancel();
}
});
POST 请求
D/OkHttp: --> POST https://API/
D/OkHttp: Content-Type: application/json
Accept: application/json
Content-Length: 61
D/OkHttp: {"email":"EMAIL","password":"****"}
--> END POST (60-byte body)
回复
D/OkHttp: <-- 400 https://API (2130ms)
server: awselb/2.0
date: Wed, 15 Jan 2020 14:36:31 GMT
content-type: text/html
content-length: 138
D/OkHttp: <html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
</body>
</html>
<-- END HTTP (138-byte body)
高级 REST 客户端的相同请求
要求
Accept: application/json
Content-Type: application/json
cookie: COOKIE
content-length: 61
POST /api/login/ HTTP/1.1
Host: HOST
Accept: application/json
Content-Type: application/json
cookie: COOKIE
content-length: 61
{"email":"EMAIL","password":"***"}
回复
> 200 OK
{
"token": "TOKEN"
}
有人可以帮助我吗?