2017-07-04 46 views
-1

我的JSON代碼:我怎樣才能獲得我的服務器正忙的JSON狀態?

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

我如何才能找到一種方式,JSON給我的狀態「服務器忙」?繁忙意味着例如:成千上萬的人使用我的服務器。

+0

的忙這個定義基本上是無用的。你的代碼如何知道有多少人在使用服務器? – tadman

+0

這是我的問題。我怎樣才能找到一種方式,讓JSON給我的狀態「服務器正忙」 – SilverBlue

+0

爲什麼你需要的狀態?你的標準是什麼? – shmosel

回答

0

如果你的服務器正忙,你不希望它花時間渲染JSON,此外,如果服務器負載是在你的心中,你可能最終把你的應用程序負載平衡器後面,那些通常一無所知JSON 。

來處理這種情況的正確方法是解析正確的HTTP狀態代碼,通常是429,見this answer

訪問狀態代碼的onResponse應該是這樣的:

@Override  
public void onSuccess(int statusCode, String content) { 
} 
0

的Http

503 - 服務不可用 503狀態代碼最常出現在非常繁忙的服務器上,並且表明服務器由於服務器過載而無法完成請求。

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(); 
        } 
       } 
      }; 
+0

我得到一個錯誤:「無法解析法‘getStatusLine()’」 – SilverBlue

+0

爲了得到狀態碼正確,你可以參考這個網址:https://stackoverflow.com/questions/22948006/http-status-code-in- android-volley-when-error-networkresponse-null-null –

+0

如果這有幫助,你可以接受答案。 –

相關問題