Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get response body in okhttp when code is 401

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/[email protected]&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?

like image 921
Nilesh Deokar Avatar asked Apr 26 '16 06:04

Nilesh Deokar


Video Answer


1 Answers

Try this:

switch (response.code()){
        case 401:
           JsonObject object=new JsonObject(response.body().string());
           String body=object.getString("msgDesc");
           break;
}
like image 86
Nongthonbam Tonthoi Avatar answered Nov 04 '22 23:11

Nongthonbam Tonthoi