Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更新error.getKind示例_Android_Retrofit - Fatal编程技术网

Android 更新error.getKind示例

Android 更新error.getKind示例,android,retrofit,Android,Retrofit,我需要从错误中捕捉错误。下面是我使用的代码 if (exception instanceof RetrofitError) { RetrofitError retrofitError = (RetrofitError) exception; m_tvStatus.setVisibility(View.VISIBLE); String msg = ""; if (retrofitError.getResponse() != null) { if (r

我需要从错误中捕捉错误。下面是我使用的代码

if (exception instanceof RetrofitError) {
    RetrofitError retrofitError = (RetrofitError) exception;
    m_tvStatus.setVisibility(View.VISIBLE);
    String msg = "";
    if (retrofitError.getResponse() != null) {
        if (retrofitError.getResponse().getStatus() > 500) {
                msg = "Network error HTTP ("
                        + retrofitError.getResponse().getStatus() + ")";
                if (retrofitError.getMessage() != null
                        && !retrofitError.getMessage().isEmpty()) {
                        msg += ": " + retrofitError.getMessage();
                }
            }else if (retrofitError.getBody() == null) {
                msg = exception.getMessage();
            } else if (retrofitError.getCause() instanceof ConnectException) {
                msg = getString(R.string.connection_error);

            } else if (retrofitError.getCause() instanceof SocketTimeoutException) {
                msg = getString(R.string.connection_timeout);
            }
        }else if (retrofitError.getKind() !=null){
            if (retrofitError.getKind().name().equalsIgnoreCase("NETWORK"))
                msg = getString(R.string.connection_timeout);
            else
                msg = getString(R.string.connection_error);
        }
        m_tvStatus.setText(msg);
   }
}
问题是如何从错误.getKind()捕获消息。在我上面的代码中,我使用硬代码equalsIgnoreCase(“网络”)来决定哪种错误


有没有更好的方法可以从infractError.getKind()捕获错误消息?

因为它是一个Java枚举值,只需使用

要获取此错误枚举的字符串表示形式,请修改error.getKind().toString()

getKind().name()

首先,
getKind()
将永远不会为
null
。它也是一个枚举,所以停止对它进行字符串比较

处理此问题的适当方法是
打开
getKind()
并采取适当的行动

switch (error.getKind()) {
  case HTTP:
    // TODO get message from getResponse()'s body or HTTP status
    break;

  case NETWORK:
    // TODO get message from getCause()'s message or just declare "network problem"
    break;

  case CONVERSION:
  case UNEXPECTED:
    throw error;

  default:
    throw new AssertionError("Unknown error kind: " + error.getKind());
}

对不起,第一段听起来很鼓舞人心,因为它让你不用做很多工作。但重读一遍,我听起来有点粗鲁!