0

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?

Community
  • 1
  • 1

1 Answers1

0

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");
    }
}
  • you mean 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!!"); } –  May 20 '17 at 13:31
  • skip this line from Your code if(error.networkResponse.data!=null) – Solaiman Hossain May 20 '17 at 13:34
  • kindly check the status & print that String statusCode = String.valueOf(error.networkResponse.statusCode); Log.e("Error", "_log : error : " + error.toString() + " " + error.networkResponse.statusCode); – Solaiman Hossain May 20 '17 at 14:04