简体   繁体   中英

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

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?

remove the "if" condition, error.networkResponse.data return 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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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