如何处理 Retrofit 2.0 中的错误 [英] How to handle error in Retrofit 2.0

查看:25
本文介绍了如何处理 Retrofit 2.0 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理 Retrofit 2.0 中的错误

得到例如code=404body=null,但 errorBody() 包含 ErrorModel 中的数据(布尔状态字符串信息).

这是errorBody().content:[text=\n{"status":false,"info":"提供的电子邮件不存在."}].

如何获取这些数据?

谢谢你帮助我!

这是我的改造请求代码:

ResetPasswordApi.Factory.getInstance().resetPassword(loginEditText.getText().toString()).enqueue(新回调(){@覆盖public void onResponse(Call call, Response response) {如果(响应.isSuccessful()){showToast(getApplicationContext(), getString(R.string.new_password_sent));} 别的 {showToast(getApplicationContext(), getString(R.string.email_not_exist));}}@覆盖public void onFailure(Call<StatusInfoModel> call, Throwable t) {showToast(getApplicationContext(), "出了点问题...");}});

解决方案

如果您想在错误响应出现时获取数据(通常是除 200 之外的响应代码),您可以在您的onResponse() 方法:

if (response.code() == 404) {Gson gson = new GsonBuilder().create();YourErrorPojo pojo = new YourErrorPojo();尝试 {pojo = gson.fromJson(response.errorBody().string(), YourErrorPojo.class);Toast.makeText(context, pojo.getInfo(), Toast.LENGTH_LONG).show();} catch (IOException e) {//处理错误解析失败}}

生成 YourErrorPojo.class 时,请执行以下步骤:

  1. 转到Json Schema 2 Pojo

  2. 粘贴您的示例Json,然后选择源类型Json,注释Gson

  3. 您的示例 Json 是:{status":false,info":提供的电子邮件不存在."}

  4. 点击预览,它会为您生成Pojo类.

将此添加到您的 build.gradle 中:compile 'com.google.code.gson:gson:2.7'

我在这个解决方案中使用了 Gson 但你可以使用 response.errorBody().string() 获取你的 Json 字符串>

I want to handle error in Retrofit 2.0

Got e.g. code=404 and body=null, but errorBody() contains data in ErrorModel (Boolean status and String info).

This is errorBody().content: [text=\n{"status":false,"info":"Provided email doesn't exist."}].

How can I get this data?

Thank for helping me!

This is my code for Retrofit request:

ResetPasswordApi.Factory.getInstance().resetPassword(loginEditText.getText().toString())
    .enqueue(new Callback<StatusInfoModel>() {
        @Override
        public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) {
            if (response.isSuccessful()) {
                showToast(getApplicationContext(), getString(R.string.new_password_sent));
            } else {
                showToast(getApplicationContext(), getString(R.string.email_not_exist));
            }
        }

        @Override
        public void onFailure(Call<StatusInfoModel> call, Throwable t) {
            showToast(getApplicationContext(), "Something went wrong...");
        }
    });

解决方案

If you want to get data when error response comes (typically a response code except 200) you can do it like that in your onResponse() method:

if (response.code() == 404) {
    Gson gson = new GsonBuilder().create();
    YourErrorPojo pojo = new YourErrorPojo();
    try {
         pojo = gson.fromJson(response.errorBody().string(), YourErrorPojo.class);
         Toast.makeText(context, pojo.getInfo(), Toast.LENGTH_LONG).show();
    } catch (IOException e) { 
      // handle failure at error parse 
  }
}

When generating YourErrorPojo.class do following steps :

  1. Go to Json Schema 2 Pojo

  2. Paste your example Json, and select source type Json , annotation Gson

  3. Your example Json is : {"status":false,"info":"Provided email doesn't exist."}

  4. Click Preview and it will generate your Pojo class for you.

Add this to your build.gradle : compile 'com.google.code.gson:gson:2.7'

I used Gson in this solution but you can get your Json string using: response.errorBody().string()

这篇关于如何处理 Retrofit 2.0 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆