2016-11-18 18 views
0

任何人都可以幫助我將arraylist帶出循環。如果我把它拿出來,它會隨着數組的越界而變空。這裏是我的代碼:無法將json值打印到整數數組列表中?是保存在那裏,但無法退出循環?

public void collapse() { 
    corequestQueue= Volley.newRequestQueue(getApplicationContext()); 
    String url="http://192.168.43.208/online/json.php"; 
    JsonObjectRequest jsonObjectRequest3=new JsonObjectRequest(Request.Method.GET, url, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 
        JSONArray jsonArray=response.getJSONArray("server_responce"); 
        for (int i=0;i<=jsonArray.length();i++){ 

         JSONObject admin=jsonArray.getJSONObject(i); 

         solution.add(Integer.valueOf(admin.getString("sno"))); 

        //if i do like this the values prints here in the toast.   
        //Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show(); 
        //printing the toast anywhere out of the block gives error.. as assay out of bound 0 on index size 0.. help me guys 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("Volly","error"); 
      } 
     } 

     ); 
    corequestQueue.add(jsonObjectRequest3); 
    Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show(); 
} 

最後一行給我的錯誤,因爲它的未來是空的,我不能把數組列表以外的任何地方。當它在循環內部並且值正在增加時,它是完美的。我已經檢查過,只有一件事是我不能把它拿出來?

+0

後,其中u已宣佈的解決方案列表? – Raghavendra

+0

請發佈,你應該收到 –

回答

0

凌空respoce會給細節只有internet.so我已經使用處理器..叫我應該execute.here是它的meathod aftercertain時間BZ。

 new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(User_Login.this, ""+solution.size(), Toast.LENGTH_SHORT).show(); 
      Collections.shuffle(solution); 
      data(); 
     } 
    },i*3000); 

我的代碼添加到JSON數組列表:我可以設置,如果條件檢查ArrayList的大小就知道哪裏有合適的網絡或不..

  try { 
         JSONArray jsonArray=response.getJSONArray("server_responce"); 
         for (int i=0;i<=jsonArray.length();i++){ 

          JSONObject admin=jsonArray.getJSONObject(i); 
          String bno = admin.getString("bno"); 
          String series=admin.getString("sno"); 
          if (bnum.equals(bno)) { 
           int k = Integer.valueOf(series); 
           solution.add(k); 
          } 

         } 
+1

U shud不使用處理程序在這裏@Bathri。這可能會導致性能問題.. –

+0

它不會在某個時間後在線程中需要時間,而不是因爲互聯網的線程功能。如果你認爲處理程序是更好的解決方案,如果你有大量的數據解析該線程,你會設置多少時間? – Raghavendra

+0

我已經使用@RushiAyyappa建議來形成我的答案。你能檢查嗎? – Raghavendra

0

希望您給您的全球Integer類型的ArrayList中。

public void collapse(){ 
solution = new ArrayList<>(); 
corequestQueue= Volley.newRequestQueue(getApplicationContext()); 
String url="http://192.168.43.208/online/json.php"; 
JsonObjectRequest jsonObjectRequest3=new JsonObjectRequest(Request.Method.GET, url, 
    new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       JSONArray jsonArray=response.getJSONArray("server_responce"); 
       for (int i=0;i<=jsonArray.length();i++){ 

        JSONObject admin=jsonArray.getJSONObject(i); 
        String val=admin.optString("sno")); 
        if(!val.equalIgnoreCase("")) 
        solution.add(Integer.parseInt(val); 

        //if i do like this the values prints here in the toast.   

        //printing the toast anywhere out of the block gives error.. as assay out of bound 0 on index size 0.. help me guys 
       } 
    Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show(); 
       if(solution.size()!=0) 
       { 
       Collections.shuffle(solution) 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, 
    new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("Volly","error"); 
     } 
    } 
); 
corequestQueue.add(jsonObjectRequest3); 

} 
+0

請你詳細說明你的答案嗎? – Raghavendra

+0

作者的代碼在將接收到的代碼表單字符串轉換爲整數時發出異常,因此arraylist爲空。我的代碼將解決這個問題。 –

+0

此解決方案不起作用。作爲JsonObjectRequest是異步調用,因此將在索引的大小肯定拋出異常'陣列外邊界0的0'當你試圖讓'solution.get(0)' – Satendra

0

事實上如是Ayyappa發佈的解決方案。我再次發佈它。

public void collapse() { 
    corequestQueue= Volley.newRequestQueue(getApplicationContext()); 
    String url="http://192.168.43.208/online/json.php"; 
    JsonObjectRequest jsonObjectRequest3=new JsonObjectRequest(Request.Method.GET, url, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       try { 
        JSONArray jsonArray=response.getJSONArray("server_responce"); 
        for (int i=0;i<=jsonArray.length();i++){ 

         JSONObject admin=jsonArray.getJSONObject(i); 

         solution.add(Integer.valueOf(admin.getString("sno"))); 

        //if i do like this the values prints here in the toast.   
        //Toast.makeText(User_Login.this, "hello"+solution.get(0), Toast.LENGTH_SHORT).show(); 
        //printing the toast anywhere out of the block gives error.. as assay out of bound 0 on index size 0.. help me guys 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       handleAfterResponse(); 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("Volly","error"); 
      } 
     } 

     ); 
    corequestQueue.add(jsonObjectRequest3); 
} 

private void handleAfterResponse() { 
if(solution != null && !solution.isEmpty()) { 
Toast.makeText(User_Login.this, ""+solution.size(), Toast.LENGTH_SHORT).show(); 

      Collections.shuffle(solution); 
      data(); 
    } 
}