-1

My JSON Code:

Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            int success = jsonResponse.getInt("success");

                            if (success == 0) {

                            }
                            if (success == 1) { 

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                nRequest nRequest = new nRequest(edtTextReg.getText().toString(), responseListener);
                RequestQueue queue = Volley.newRequestQueue(login.this);
                queue.add(nRequest);

How can I find a way that JSON give me the status "server is busy"? Busy means for example: thousands of people use my server.

SilverBlue
  • 239
  • 2
  • 13

2 Answers2

1

Http status code for server overload is 503.

503 - Service Unavailable A 503 status code is most often seen on extremely busy servers, and it indicates that the server was unable to complete the request due to a server overload.

so your code should be something like this:

                @Override

                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);

                 int status = jsonResponse.getStatusLine().getStatusCode();
                      int success = jsonResponse.getInt("success");
                        if (status == 503) {
                          // 
                        }

                        if (success == 0) {

                        }
                        if (success == 1) { 

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };
  • I get an error: "Cannot resolve Method 'getStatusLine()'" – SilverBlue Jul 05 '17 at 14:16
  • For getting the status code correctly you could refer to this url: https://stackoverflow.com/questions/22948006/http-status-code-in-android-volley-when-error-networkresponse-is-null – Sakti Behera Jul 05 '17 at 17:57
0

If your server is busy you don't want it to spend time rendering a JSON, moreover, if server load is in your mind you might end up putting your application behind a load-balancer and those usually knows nothing about JSON.

The proper way to handle this situation is to parse the correct HTTP status code, usually 429, see this answer

To access the status code your onResponse should look like this:

@Override    
public void onSuccess(int statusCode, String content) {
}
ApriOri
  • 2,618
  • 29
  • 48