简体   繁体   English

用齐射捕获服务器关闭错误

[英]Catch server down error with volley

I want to know if the server is down or not when i do a request, basicly i am trying to do a simple register(it already works) but if put the nodeJS server down when i click the register it doesn't appear any error on my toast, i tried to follow some answers that i found but nothing works 我想知道我执行请求时服务器是否关闭,基本上我正在尝试做一个简单的注册(它已经可以工作了),但是如果我单击注册时放下NodeJS服务器,它不会出现任何错误在我的吐司面包上,我尝试遵循一些发现的答案,但是没有任何效果

Here is what i tried: 这是我尝试过的:

 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");
                    }
                }

For any question related to how i construct the volley example i followed this thread 对于与我如何构造齐射示例有关的任何问题,我都遵循此线程

The messages inside the NEtwork error and serverError never show, any tip? NEtwork错误和serverError内的消息永不显示,有任何提示吗?

remove the "if" condition, error.networkResponse.data return null. 删除“如果”条件,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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM