2013-12-23 69 views
2

只有最後一個值我有一個整數數組需要被稱爲一個又一個從URL中的循環。我有下面的代碼,它調用了最後一個號碼多次,但所有的數字從一開始就跳過了。我如何使這個工作。我真的很感激任何幫助。感謝提前。循環的AsyncTask Android是執行環路

final String[] ar={"1","2","3",.............,"25"} 

       for ( j = 0; j < ar.length; j++) { 


         u="http://www.example.com/"+ar[j]; 

         JSONParser jParser=new JSONParser();   
         new MyAsyncTask().execute(u); 

       } 


    class MyAsyncTask extends AsyncTask<String, String, Void> { 

     private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); 
     InputStream inputStream = null; 
     String result = ""; 

     protected void onPreExecute() { 
      progressDialog.setMessage("Downloading your data..."); 
      progressDialog.show(); 
      progressDialog.setOnCancelListener(new OnCancelListener() { 
       public void onCancel(DialogInterface arg0) { 
        MyAsyncTask.this.cancel(true); 
       } 
      }); 
     } 

     @Override 
     protected Void doInBackground(String... params) { 

      String url_select = params[0]; 
      try { 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpResponse httpResponse = httpclient.execute(new HttpGet(url_select)); 

       // receive response as inputStream 
       inputStream = httpResponse.getEntity().getContent(); 
// 
//    // Read content & Log 
//    inputStream = httpEntity.getContent(); 
      } catch (UnsupportedEncodingException e1) { 
       Log.e("UnsupportedEncodingException", e1.toString()); 
       e1.printStackTrace(); 
      } catch (ClientProtocolException e2) { 
       Log.e("ClientProtocolException", e2.toString()); 
       e2.printStackTrace(); 
      } catch (IllegalStateException e3) { 
       Log.e("IllegalStateException", e3.toString()); 
       e3.printStackTrace(); 
      } catch (IOException e4) { 
       Log.e("IOException", e4.toString()); 
       e4.printStackTrace(); 
      } 
      // Convert response to string using String Builder 
      try { 
       BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8); 
       StringBuilder sBuilder = new StringBuilder(); 

       String line = null; 
       while ((line = bReader.readLine()) != null) { 
        sBuilder.append(line + "\n"); 
       } 

       inputStream.close(); 
       result = sBuilder.toString(); 

      } catch (Exception e) { 
       Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString()); 
      } 
      return null; 
     } // protected Void doInBackground(String... params) 


     protected void onPostExecute(Void v) { 

      //parse JSON data 
      try{ 
       JSONObject jArray = new JSONObject(result); 


       // End Loop 

       this.progressDialog.dismiss(); 

      } catch (JSONException e) { 

       Log.e("JSONException", "Error: " + e.toString()); 

      } // catch (JSONException e) 
     catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } // protected void onPostExecute(Void v) 

    } //class MyAsyncTask extends AsyncTask<String, String, Void> 

回答

1

使用

String url_select = parms[0]; 

如果你想並行執行,你應該使用一個執行者。我建議你使用執行程序,而不是在for循環中執行asynctask。

new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "url1","url2"..); 

http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor,參數...)

檢查的執行順序@

http://developer.android.com/reference/android/os/AsyncTask.html

+0

得到傳遞值的URL是動態的,我怎樣才能使爲url1,url2 URL3 ... – jason

+0

@jason url1,url2就是一個例子。無論其動態與否都無關緊要。如果您不關心執行順序,最好使用執行程序進行並行執行。如果您對訂單感到困擾,可以使用串行執行程序。 – Raghunandan

+0

真棒+ 1.Thanks – jason

2

在doInBackground你應該使用

url_select = params[0]; 

,而不是 url_select = U ; (您在for循環中將u賦值爲異步執行任務,因此您無法真正知道哪個值具有,當任務執行時......並最終保留您分配的最後一個值。)

+0

將這個呼叫工作\t 「新MyAsyncTask()執行(U)」; – jason

+0

是的,該電話是正確的。和您傳遞給執行帕拉姆將在doInBackground可作爲PARAM [0] – jpm

1

String url_select = u;

我認爲這個問題是在這裏您使用的是硬編碼值在這裏您應該params

String url_select = params[0];

+0

將這個呼叫工作\t「新MyAsyncTask()執行(U)」。 – jason

+0

是的,應該工作 –