loop asynctask android只在循环中执行最后一个值

时间:2013-12-23 13:52:44

标签: android android-asynctask getjson http-get

我有一个整数数组,需要在来自url的循环中一个接一个地调用。我有下面的代码,它会多次调用最后一个数字,但是从头开始的所有数字都被跳过。如何制作这是工作。我真的很感激任何帮助。谢谢你。

 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>

3 个答案:

答案 0 :(得分:2)

在doInBackground中你应该使用

url_select = params[0];
而不是  url_select = u; (你在异步执行任务时在for循环中分配你,所以你不能'真正知道你有哪个值,执行任务时......最终它保留你分配的最后一个值..)

答案 1 :(得分: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,Params ......)

检查执行顺序@

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

答案 2 :(得分:1)

String url_select = u;

我认为问题在于你在这里使用硬编码值你应该从params获得传递的值

String url_select = params[0];