how to get response body in okhttp when code is 401

跟風遠走 提交于 2019-12-22 04:18:23

问题


i am using okHttp 3.2.0 and here is code for building request object

MediaType JSON = MediaType.parse(AppConstants.CONTENT_TYPE_VALUE_JSON);
RequestBody body = RequestBody.create(JSON, requestBody);


    HttpUrl url = new HttpUrl.Builder()
                                    .scheme("http")
                                    .host("192.168.0.104")
                                    .port(8080)
                                    .addPathSegment("mutterfly-server")
                                    .addPathSegment("j_spring_security_check")
                                    .addQueryParameter("j_username", jsonObject.getString("emailId"))
                                    .addQueryParameter("j_password", jsonObject.getString("password"))
                                    .build();

     request = new Request.Builder()
                                    .addHeader(AppConstants.CONTENT_TYPE_LABEL, AppConstants.CONTENT_TYPE_VALUE_JSON)
                                    .addHeader(AppConstants.ACCEPT_LABEL, AppConstants.CONTENT_TYPE_VALUE_JSON)
                                    .url(url)
                                    .post(body)
                                    .build();

And here is how i parse the response

client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, final Response response) throws IOException {

                    String respBody;
                    if (response.isSuccessful()) {
                        if (response.body() != null) {
                            respBody = response.body().string();
                            Log.i(TAG, respBody);
                            response.body().close();
                            if (AppMethods.checkIfNull(loginParserListener)) {
                                try {
                                    final VUser user = AppMethods.getGsonInstance().fromJson(respBody, VUser.class);
                                } catch (Exception e) {

                                }
                            }
                        }
                    } else {

                        switch (response.code()){
                            case 401:
                                String body="HTTP_UNAUTHORIZED";
                                break;
                        }
                    }
                }
            });

This is the ideal response(from web rest client) when authentication is failed.

{"msgDesc":"The username or password you entered is incorrect..","statusCode":401}

EDIT:

response.toString() returns

Response{protocol=http/1.1, code=401, message=Unauthorized, url=http://192.168.0.104:8080/mutterfly-server/j_spring_security_check?j_username=s@s.s&j_password=1}

response.body().toString() returns

okhttp3.internal.http.RealResponseBody@528ae030

i want to fetch the 'msgDesc' which is in response body. is there any method which will return this string?


回答1:


Try this:

switch (response.code()){
        case 401:
           JsonObject object=new JsonObject(response.body().string());
           String body=object.getString("msgDesc");
           break;
}



回答2:


It's quite weird but Square, the company behind OkHttp, has chosen to not use 'toString()' but 'string()' as method for getting the body as a String.

So this works;

String string = response.body().string();
//convert to JSON and get your value

But this doesn't:

String string = response.body().toString();



回答3:


401 means permission denied.

Check if your token is valid or user/password is correct.



来源:https://stackoverflow.com/questions/36856820/how-to-get-response-body-in-okhttp-when-code-is-401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!