【问题标题】:Catch server down error with volley截击服务器停机错误
【发布时间】:2017-05-20 12:20:10
【问题描述】:

我想知道当我发出请求时服务器是否关闭,基本上我正在尝试做一个简单的注册(它已经工作)但是如果当我单击注册时将 nodeJS 服务器关闭它不会在我的吐司上出现任何错误,我试图按照我找到的一些答案进行操作,但没有任何效果

这是我尝试过的:

 public void notifyError(String requestType,VolleyError error) {
                String body;
                if(error.networkResponse.data!=null) {
                    String statusCode = String.valueOf(error.networkResponse.statusCode);
                    try {
                        if (error instanceof NetworkError) {
                            Log.d("internet","nao tem internet ligada");
                        }
                        else if (error instanceof ServerError) {
                            Log.d("internet","The server could not be found. Please try again after some time!!");
                        }
                        body = new String(error.networkResponse.data,"UTF-8");
                        JSONObject jsonObj = new JSONObject(body);
                        Log.d("body",String.valueOf(jsonObj.get("message")));
                        showToast(String.valueOf(jsonObj.get("message")));
                    } catch (UnsupportedEncodingException e) {
                        showToast("You need to connect to the internet!");
                    } catch (JSONException e) {
                        Log.d("json:","problems decoding jsonObj");
                    }
                }

关于我如何构建凌空示例的任何问题,我都遵循了这个thread

NEtwork error 和 serverError 中的消息从不显示,有什么提示吗?

【问题讨论】:

    标签: java android android-volley


    【解决方案1】:

    去掉“if”条件,error.networkResponse.data 返回null。

    public void notifyError(String requestType,VolleyError error) {
        String body;
        String statusCode = String.valueOf(error.networkResponse.statusCode);
        try {
            if (error instanceof NetworkError) {
                Log.d("internet","nao tem internet ligada");
            } else if (error instanceof ServerError) {
                Log.d("internet","The server could not be found. Please try again after some time!!");
            }
    
            body = new String(String.valueOf(error.networkResponse.statusCode),"UTF-8"); 
            JSONObject jsonObj = new JSONObject(body);
            Log.d("body",String.valueOf(jsonObj.get("message")));
            showToast(String.valueOf(jsonObj.get("message")));
        } catch (UnsupportedEncodingException e) {
            showToast("You need to connect to the internet!");
        } catch (JSONException e) {
            Log.d("json:","problems decoding jsonObj");
        }
    }
    

    【讨论】:

    • 你的意思是 if (error instanceof NetworkError) { Log.d("internet","nao tem internet ligada"); } else if (error instanceof ServerError) { Log.d("internet","找不到服务器。请过一段时间再试!!"); }
    • 从你的代码中跳过这一行 if(error.networkResponse.data!=null)
    • 请检查状态并打印 String statusCode = String.valueOf(error.networkResponse.statusCode); Log.e("Error", "_log : error : " + error.toString() + " " + error.networkResponse.statusCode);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多