Retrofit 2.0 - How to get response body for 400 Bad Request error?

前端 未结 9 2079
旧巷少年郎
旧巷少年郎 2021-01-31 02:33

So when I make a POST API call to my server, I get a 400 Bad Request error with JSON response.

{
    \"userMessage\": \"Blah\",
    \"internalMessage\": \"Bad Re         


        
9条回答
  •  北荒
    北荒 (楼主)
    2021-01-31 03:04

    public void onResponse(Call call, Response response) {
        DialogHelper.dismiss();
    
        if (response.isSuccessful()) {
            // Success
        } else {
            try {
                JSONObject jObjError = new JSONObject(response.errorBody().string());
                Toast.makeText(getContext(), jObjError.getString("message"), Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
    

提交回复
热议问题