how to get response body in okhttp when code is 401

拜拜、爱过 提交于 2019-12-05 02:45:20

Try this:

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

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();

401 means permission denied.

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

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