首页 > 解决方案 > 如何处理服务器连接错误和互联网连接错误并在 volley android 中为每一个显示不同的消息?

问题描述

我想要的是针对连接错误显示不同的消息,当出现服务器连接错误时,我的意思是向用户显示存在服务器问题,如果没有互联网告诉用户没有互联网连接,我如何通过 volley 实现这一点?

我已经尝试了以下代码:

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

标签: androiderror-handlingandroid-volleygeneral-network-error

解决方案


'这是解决方案'

            @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);

                    }

'希望你能得到答案'


推荐阅读