【问题标题】:how to handle server connection error and internet connection error and show different messages for each one in volley android?如何处理服务器连接错误和互联网连接错误并在 volley android 中为每一个显示不同的消息?
【发布时间】:2019-10-15 20:44:42
【问题描述】:

我想要为连接错误显示不同的消息,我的意思是当出现服务器连接错误时向用户显示存在服务器问题,如果没有互联网告诉用户没有互联网连接,我该如何实现那是凌空吗?

我已经尝试了以下代码:

public void onErrorResponse(VolleyError error) {
    if (error instanceof TimeoutError) {
        Snackbar.make(contextView,getString(R.string.no_internen_connection_msg),Snackbar.LENGTH_LONG).show();
    } else if (error instanceof NetworkError) {
        Snackbar.make(contextView,getString(R.string.no_internen_connection_msg),Snackbar.LENGTH_LONG).show();
    } else if (error instanceof NoConnectionError) {
        Snackbar.make(contextView,getString(R.string.server_con_err_msg),Snackbar.LENGTH_LONG).show();
    } else {      
        if (parseNetworkError(error).getMessage() != null) {
            //do something
        }else {
            //do something
        }
    }
}

NoConnectionError 的条件显示此警告“条件error instanceof NoConnectionError 始终为假”

即使有互联网连接,它也总是显示互联网连接错误,但问题是服务器连接error

【问题讨论】:

    标签: android error-handling android-volley general-network-error


    【解决方案1】:

    '这是解决方案'

                @Override
                public void onErrorResponse(VolleyError error) {
                    String json = null;
                    String json_js = null;
    
    
                    NetworkResponse response = error.networkResponse;
                    if(response != null && response.data != null){
                        int codee=response.statusCode;
                        switch(response.statusCode){
                            case 400:
                                json = new String(response.data);
                                json = trimMessage(json, "message");
                                if(json != null) displayMessage(json);
    //                            json_js=json;
    //                            JSONObject jj=json;
                                callback.onSuccess(json_js,json);
                                break;
                            case 401:
                                callback.onSuccess(json_js,"401");
                                break;
                            case 405:
    
                                callback.onSuccess(json_js,"Sending Wrong Request");
                                break;
                            default:
                                callback.onSuccess(json_js,error.toString());
    
                        }
                        //Additional cases
                    }
                    else {
                        String message;
                        if (error instanceof NetworkError) {
                            message = "Cannot connect to Internet...Please check your 
                            connection!";
                            callback.onSuccess(json_js, message);
    
                        } else if (error instanceof ServerError) {
                            message = "The server could not be found. Please try again 
                                 after some time!!";
                            callback.onSuccess(json_js, message);
    
                        } else if (error instanceof AuthFailureError) {
                            message = "Cannot connect to Internet...Please check your 
                              connection!";
                            callback.onSuccess(json_js, message);
    
                        } else if (error instanceof ParseError) {
                            message = "Parsing error! Please try again after some 
                             time!!";
                            callback.onSuccess(json_js, message);
    
                        } else if (error instanceof NoConnectionError) {
                            message = "Cannot connect to Internet...Please check your 
                             connection!";
                            callback.onSuccess(json_js, message);
    
                        } else if (error instanceof TimeoutError) {
                            message = "Connection TimeOut! Please check your internet 
                            connection.";
                            callback.onSuccess(json_js, message);
    
                        }
    

    '希望你能得到答案'

    【讨论】:

    • 抱歉您的回答没有帮助到我,仍然无法解决我的问题
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多