Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用wifi时,多个异步功能无法完美工作,但在移动网络上却可以完美工作_Android_Asynchronous_Android Asynctask - Fatal编程技术网

Android 使用wifi时,多个异步功能无法完美工作,但在移动网络上却可以完美工作

Android 使用wifi时,多个异步功能无法完美工作,但在移动网络上却可以完美工作,android,asynchronous,android-asynctask,Android,Asynchronous,Android Asynctask,我在OnCreate()中执行了3个异步调用。我正在执行所有3个异步调用,用于获取数据和更新listview。这在移动网络上非常有效,但在Wifi上有时不起作用。此外,我猜它是并行发生的 请让我知道为什么它不能在wifi上完美工作。也请让我知道这是否适合一次执行3个异步调用。我非常感谢任何帮助。提前感谢 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我在OnCreate()中执行了3个异步调用。我正在执行所有3个异步调用,用于获取数据和更新listview。这在移动网络上非常有效,但在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
中,我看到您正在获取连接超时,因此您不应该期望列表中有任何数据。相反,您应该处理此错误案例,以通知用户有错误。

您也可以在帖子中提到问题所在,说它工作不完美是一个有点宽泛的评论。@Niko Hi Niko。进度对话框一直显示,然后随着toast“完成”消失但它不会更新listview中的任何数据。但有时它会更新。我不知道为什么?我需要查看更多代码,您如何处理适配器数据更新?@Niko继续更新其余代码。可能您的asynctask会覆盖其他asynctask结果?很抱歉,这是一个输入错误。它是在上面的q中编辑的。还有什么我需要更改的吗。
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)