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

时间:2012-08-22 10:49:49

标签: 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();
        }

    }

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

2 个答案:

答案 0 :(得分: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();
        }

    }

现在你可以使用你的LIST&amp; LIST1,如果你想要它。您也可能不需要在DoInBackground中返回arraylist。

希望它会帮助你。

答案 1 :(得分:3)

创建一个包含两个列表的新类。使用该类作为返回类型。