xiaoyan_12 2013-07-22 05:36 采纳率: 25%
浏览 2824

使用 async task 如何返回对象?

我想从 async task 里解析后返回 tweets 的列表,但是我从 task 中没有返回 arraylist。谁能提供一个解决方案啊?

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList listItems = new ArrayList();
        new myAsyncTask().execute(listItems);

        setListAdapter(new ArrayAdapter(this, R.layout.tweet, R.id.tweet,listItems));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // When clicked, show a Toaster
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class myAsyncTask extends AsyncTask<ArrayList<Object>, Void, Void>
    {
        ArrayList<Object> listItems;
        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(Main.this, "", "Loading....");
        }
        @Override
        protected Void doInBackground(ArrayList<Object>... params) {

            String host = "api.twitter.com";
            String twitterURL = "http://"+host+"/1/statuses/user_timeline.json?screen_name=i1990jain&amp;count;=10";
            try {
                HttpClient client = new DefaultHttpClient();
                BasicHttpContext localContext = new BasicHttpContext();
                HttpHost targetHost = new HttpHost(host, 80, "http");
                HttpGet httpget = new HttpGet(twitterURL);
                httpget.setHeader("Content-Type", "application/json");
                HttpResponse response = client.execute(targetHost, httpget, localContext);
                HttpEntity entity = response.getEntity();
                Object content = EntityUtils.toString(entity);
                Log.d(MY_APP_TAG, "OK: " + content.toString());

                JSONArray ja = new JSONArray(content.toString());

                for(int i = 0; i < ja.length(); i++){
                    JSONObject jo = ja.getJSONObject(i);
                    listItems.add(jo.getString("text"));
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            dialog.dismiss();
        }
    }
}
  • 写回答

2条回答 默认 最新

  • JaveZh 2013-07-22 05:46
    关注

    把doInBackground中的最后一句return null改为return listItems呢

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值