Android AsyncTask在doInBackground中返回多个值以用于onPostExecute

KaH*_*HeL 8 android

我刚刚阅读并测试了AsyncTask,现在我需要知道如何在onPostExecute部分传递多个值.我使用JSON解析器从Web获取值,但是我从JSON获取的值是多个值,我将这些值传递给一个数组,该数组由每个获取的数据的列标题分隔,这是我应该返回它的部分用于onPostExecute使用.但据我所知,每次运行只能使用一次返回(如果我错了,请纠正我).

那么这是我的代码到目前为止:

public class GetInfo extends AsyncTask<String, Void, List<String>>{

        private final String TAG = null;
        InputStream is = null;
        StringBuilder sb=null;
        List<String> list = new ArrayList<String>();

        @Override
        protected List<String> doInBackground(String... url) {
            String result = "";

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                //CONNECT TO DATABASE
                 try{
                         HttpClient httpclient = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost(url[0]);
                         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                         HttpResponse response = httpclient.execute(httppost);
                         HttpEntity entity = response.getEntity();
                         is = entity.getContent();
                         Log.v(TAG, "connected");
                 }catch(Exception e){
                         Log.v(TAG, "run failed");

                         Log.e("log_tag", "Error in http connection "+e.toString());
                 }

                 //BUFFERED READER FOR INPUT STREAM
                try{
                     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                     sb = new StringBuilder();
                     String line = "0";

                     while ((line = reader.readLine()) != null) {
                             sb.append(line + "\n");
                     }
                     is.close();
                     result=sb.toString();
                     Log.v(TAG, "buffered read");
                 }catch(Exception e){
                     Log.v(TAG, "buffered error");
                         Log.e("log_tag", "Error converting result "+e.toString());
                 }

                //CONVERT JSON TO STRING
                try{
                     Log.v(TAG, result);

                         JSONArray jArray = new JSONArray(result);
                         JSONObject json_data=null;

                         for(int i=0;i<jArray.length();i++){
                             Log.v(TAG, "loop start");

                                 json_data = jArray.getJSONObject(i);
                                 list.add(json_data.getString("id"));
                                 list2.add(json_data.getString("city"));
                                 Log.v(TAG, "list added");
                         }

                 }catch(JSONException e){
                     Log.v(TAG, "rest failed");
                         Log.e("log_tag", "Error parsing data "+e.toString());
                 }

                 Log.v(TAG, list.toString());

                return list; //I also need to return the list2 here

        }

        @Override
        protected void onPostExecute(List<String> result) {
            cities = result; //lost in this part hahaha!
            showCities();
        }

    }
Run Code Online (Sandbox Code Playgroud)

好吧补充一点,当我只返回一个字符串数组(列表)时,这段代码工作正常,但我现在在第二部分感到困惑.此外,城市在Main类中声明,ShowCities()仅用于显示.所以我不打扰添加代码.

Bha*_*iya 5

你可以做一件事使你的ArrayList成为静态,并在你想要它时访问它.

public static List<String> LIST = new ArrayList<String>();
public static List<String> LIST1 = new ArrayList<String>();

public class GetInfo extends AsyncTask<String, Void, List<String>>{

        private final String TAG = null;
        InputStream is = null;
        StringBuilder sb=null;


        @Override
        protected List<String> doInBackground(String... url) {
            String result = "";

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                //CONNECT TO DATABASE
                 try{
                         HttpClient httpclient = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost(url[0]);
                         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                         HttpResponse response = httpclient.execute(httppost);
                         HttpEntity entity = response.getEntity();
                         is = entity.getContent();
                         Log.v(TAG, "connected");
                 }catch(Exception e){
                         Log.v(TAG, "run failed");

                         Log.e("log_tag", "Error in http connection "+e.toString());
                 }

                 //BUFFERED READER FOR INPUT STREAM
                try{
                     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                     sb = new StringBuilder();
                     String line = "0";

                     while ((line = reader.readLine()) != null) {
                             sb.append(line + "\n");
                     }
                     is.close();
                     result=sb.toString();
                     Log.v(TAG, "buffered read");
                 }catch(Exception e){
                     Log.v(TAG, "buffered error");
                         Log.e("log_tag", "Error converting result "+e.toString());
                 }

                //CONVERT JSON TO STRING
                try{
                     Log.v(TAG, result);

                         JSONArray jArray = new JSONArray(result);
                         JSONObject json_data=null;

                         for(int i=0;i<jArray.length();i++){
                             Log.v(TAG, "loop start");

                                 json_data = jArray.getJSONObject(i);
                                 LIST.add(json_data.getString("id"));
                                 LIST1.add(json_data.getString("city"));
                                 Log.v(TAG, "list added");
                         }

                 }catch(JSONException e){
                     Log.v(TAG, "rest failed");
                         Log.e("log_tag", "Error parsing data "+e.toString());
                 }

                 Log.v(TAG, list.toString());

                return LIST; //I also need to return the list2 here

        }

        @Override
        protected void onPostExecute(List<String> result) {
            cities = result; //lost in this part hahaha!
            showCities();
        }

    }
Run Code Online (Sandbox Code Playgroud)

现在,您可以在需要时使用LIST和LIST1.您也可能不需要在DoInBackground中返回arraylist.

希望它会帮助你.