使用wifi时,多个异步不能正常工作,但完美地在移动网络上工作

时间:2014-02-18 19:30:51

标签: android asynchronous android-asynctask

我在OnCreate()中执行了3个异步调用我正在执行所有3个获取数据并更新列表视图。这在移动网络上完美运行但在Wifi上它不能正常工作。此外,我猜它正在发生并行。

请让我知道为什么它在wifi上不能完美运行。如果这对于一次执行3个异步调用是完美的,请告诉我。我非常感谢任何帮助。谢谢你。

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);


         listview1 = new ListView(mContext);
         listview2 = new ListView(mContext);
         listview3 = new ListView(mContext);


         ////


            arraylist1 = new ArrayList<HashMap<String, String>>();

            new LoadInbox1().execute();

                        arraylist2 = new ArrayList<HashMap<String, String>>();

            new LoadInbox2().execute();


                        arraylist3 = new ArrayList<HashMap<String, String>>();

            new LoadInbox3().execute();

    }





    class LoadInbox1 extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Inbox JSON: ", json.toString());

            try {
                inbox = json.getJSONArray(TAG_MESSAGES);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String from = c.getString(TAG_FROM);
                    String subject = c.getString(TAG_SUBJECT);
                    String date = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_FROM, from);
                    map.put(TAG_SUBJECT, subject);
                    map.put(TAG_DATE, date);

                    // adding HashList to ArrayList
                    arraylist1.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */

                     listview1.setAdapter(new SimpleAdapter(
                                MainActivity.this, arraylist1,
                                R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT, TAG_DATE },
                                new int[] { R.id.from, R.id.subject, R.id.date }));


                }
            });

        }

    }






    class LoadInbox2 extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Inbox JSON: ", json.toString());

            try {
                inbox = json.getJSONArray(TAG_MESSAGES);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String from = c.getString(TAG_FROM);
                    String subject = c.getString(TAG_SUBJECT);
                    String date = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_FROM, from);
                    map.put(TAG_SUBJECT, subject);
                    map.put(TAG_DATE, date);

                    // adding HashList to ArrayList
                    arraylist2.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */

                     listview2.setAdapter(new SimpleAdapter(
                                MainActivity.this, arraylist2,
                                R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT, TAG_DATE },
                                new int[] { R.id.from, R.id.subject, R.id.date }));


                }
            });

        }

    }




    class LoadInbox3 extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Inbox JSON: ", json.toString());

            try {
                inbox = json.getJSONArray(TAG_MESSAGES);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String from = c.getString(TAG_FROM);
                    String subject = c.getString(TAG_SUBJECT);
                    String date = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_FROM, from);
                    map.put(TAG_SUBJECT, subject);
                    map.put(TAG_DATE, date);

                    // adding HashList to ArrayList
                    arraylist3.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */

                     listview3.setAdapter(new SimpleAdapter(
                                MainActivity.this, arraylist3,
                                R.layout.inbox_list_item, new String[] { TAG_FROM, TAG_SUBJECT, TAG_DATE },
                                new int[] { R.id.from, R.id.subject, R.id.date }));


                }
            });

        }

    }

logcat的

02-19 01:54:46.574: W/System.err(32377): java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
02-19 01:54:46.584: W/System.err(32377):    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
02-19 01:54:46.584: W/System.err(32377):    at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
02-19 01:54:46.584: W/System.err(32377):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
02-19 01:54:46.584: W/System.err(32377):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-19 01:54:46.584: W/System.err(32377):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-19 01:54:46.584: W/System.err(32377):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
02-19 01:54:46.584: W/System.err(32377):    at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
02-19 01:54:46.584: W/System.err(32377):    at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
02-19 01:54:46.594: W/System.err(32377):    at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
02-19 01:54:46.594: W/System.err(32377):    at java.io.InputStreamReader.read(InputStreamReader.java:233)
02-19 01:54:46.594: W/System.err(32377):    at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
02-19 01:54:46.594: W/System.err(32377):    at java.io.BufferedReader.readLine(BufferedReader.java:397)
02-19 01:54:46.594: W/System.err(32377):    at com.example.example.MainActivity$list3.doInBackground(MainActivity.java:776)
02-19 01:54:46.594: W/System.err(32377):    at com.example.example.MainActivity$list3.doInBackground(MainActivity.java:1)
02-19 01:54:46.594: W/System.err(32377):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-19 01:54:46.604: W/System.err(32377):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-19 01:54:46.604: W/System.err(32377):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-19 01:54:46.604: W/System.err(32377):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-19 01:54:46.604: W/System.err(32377):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-19 01:54:46.604: W/System.err(32377):    at java.lang.Thread.run(Thread.java:841)
02-19 01:54:46.604: W/System.err(32377): Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
02-19 01:54:46.614: W/System.err(32377):    at libcore.io.Posix.recvfromBytes(Native Method)
02-19 01:54:46.614: W/System.err(32377):    at libcore.io.Posix.recvfrom(Posix.java:141)
02-19 01:54:46.614: W/System.err(32377):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
02-19 01:54:46.614: W/System.err(32377):    at libcore.io.IoBridge.recvfrom(IoBridge.java:506)

1 个答案:

答案 0 :(得分:0)

LogCat我看到你的连接超时,所以你不应该期待列表中的任何数据。相反,您应该处理此错误情况以通知用户存在错误。