【问题标题】:How to Get Error Body Response with Retrofit 2如何使用 Retrofit 2 获得错误主体响应
【发布时间】:2019-03-13 11:56:16
【问题描述】:

我正在使用 Retrofit 2,我需要处理 JSON 格式的响应错误。以下是响应正文的示例。

{
    "success": false,
    "error": {
        "message": {
            "name": [
                "This input is required."
            ]
        }
    }
}

message 包含有错误的字段列表,这意味着该值是动态的。因此,一种可能的解决方案是将响应正文解析为 JSON 对象。我尝试使用 response.errorBody().string()

获取错误正文
@Override
public void onResponse(final Call<Category> call, final Response<Category> response) {
    if (response.isSuccessful()) {

    } else {
        // Handle error
        String errorBody = response.errorBody().string();
    }
}

不幸的是,打印errorBody我只能得到以下结果 {"success":false,"error":{"message":{"name":[""]}}} Retrofit 是否限制了 errorBody 对象的深度?我应该怎么做才能得到可以解析的完整响应体?

【问题讨论】:

  • 试试这样写:String errorBody = response.errorBody().string(); if true 语句中的 else 之前。
  • @UmangBurman 为什么要在此处添加response.errorBody().string();?如果响应为 4xx,则不会通过 if true 块
  • @NoName2 你解决了吗?我有完全相同的问题。
  • 解决方案在这里https://stackoverflow.com/a/70135493/4882029 它对我来说很好。

标签: android retrofit retrofit2


【解决方案1】:

只需使用response.message()

@Override
public void onResponse(final Call<Category> call, final Response<Category> response) {
    if (response.isSuccessful()) {

    } else {
        // Handle error
        Log.i("Response message: ", response.message());
    }
}

【讨论】:

  • 正在打印response.message(),我得到了Unprocessable Entity
  • 这不是信息。我想要的是响应正文
  • response.errorBody().string() 给出错误响应。 response.message() 没有给出错误信息。
【解决方案2】:

如果您尝试 response.body.toString(),我认为您将得到完整的响应。

【讨论】:

  • 尝试 { 字符串错误 = response.errorBody().string(); error = error.replace("\"", ""); Toast.makeText(getContext(), error, Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); }跨度>
【解决方案3】:

试试这个 sn-p 我在改造中使用了这个。并获取动态错误消息解决方案

@Override
public void onResponse(final Call<Category> call, final Response<Category> response) {
    if (response.isSuccessful()) {

    } else {
        try {
            String errorBody = response.errorBody().string();

            JSONObject jsonObject = new JSONObject(errorBody.trim());

            jsonObject = jsonObject.getJSONObject("error");

            jsonObject = jsonObject.getJSONObject("message");

            Iterator<String> keys = jsonObject.keys();
            String errors = "";
            while (keys.hasNext()) {
                String key = keys.next();
                JSONArray arr = jsonObject.getJSONArray(key);
                for (int i = 0; i < arr.length(); i++) {
                    errors += key + " : " + arr.getString(i) + "\n";
                }
            }
            Common.errorLog("ERRORXV", errors);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

【讨论】:

  • 可以解析 JSON 对象。但问题出在response.errorBody string 我没有得到完整的响应正文对象,正如我在问题上所说的那样
  • String message = jsonObject.getString("message");
猜你喜欢
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 2016-06-17
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多