HTTP请求在Android应用中返回“错误请求”(代码400),但可用于邮递员(代码200)

时间:2020-01-15 19:59:21

标签: android gson httprequest okhttp retrofit2.6

我在android应用上有一个API,它可以工作很长时间。系统得到更新,现在我在同一请求中收到“ 400-Bad Request”(无更改)。 但是在Postman或Advanced REST Client中效果很好。

我正在使用: Java安卓 改造2 好的HTTP GSON

Android应用

接口LoginService

@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"
}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

我没有通过“ 系统更新”来表达您的观点。如果您的意思是 Android操作系统,也许this answer可以为您提供帮助

答案 1 :(得分:0)

我找到了解决方案!

我在翻新设置中插入了 header Authorization ,它运行良好。 将Laravel更新到6.0版时,插入空的“授权” (此api /登录名是为了生成此令牌)时,我返回了错误“ 400-错误的请求”

在没有令牌的情况下,我只是删除了“ Authorization” 而已,现在,它很完美! \ o /

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(HttpLoggingInterceptor.Level.BODY);

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
        .addInterceptor(chain -> {
            Request request;
            if (LoginManager.getToken().length() > 5) {
                request = chain.request().newBuilder()
                        .header("Authorization", LoginManager.getToken())
                        .build();
            } else {
                request = chain.request().newBuilder().build();
            }
            return chain.proceed(request);
        })