1

我正在使用 j 的异步库进行发布/获取(我自己尝试过,同样的事情发生了),我一直遇到问题。当我调用 getNews() 方法时,我的 ProgressBar(不确定)一直冻结。我在这里做错了什么?

protected static ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();

public void getNews() throws JSONException
{

    VestiHTTPClient.get("json.php", null, new JsonHttpResponseHandler()
    {

        @Override
        public void onSuccess(JSONArray news)
        {
            try
            {
                for(int i=0; i<news.length(); i++)
                {
                    JSONObject jo = (JSONObject) news.get(i);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POST_URL", jo.getString("POST_URL"));
                    map.put("TOPIC_TITLE", jo.getString("TOPIC_TITLE"));
                    map.put("AUTHOR", jo.getString("AUTHOR"));
                    map.put("CATEGORY", jo.getString("CATEGORY"));
                    map.put("POST_TIME", jo.getString("POST_TIME"));
                    map.put("POST_TEXT", jo.getString("POST_TEXT"));
                    map.put("IMAGE", jo.getString("IMAGE"));

                    allNews.add(map);                       
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

        @Override
         public void onFinish() {
            Intent main = new Intent(SplashScreen.this, MainWindow.class);
            SplashScreen.this.startActivity(main);
            SplashScreen.this.finish();
         }
    });

}

一切都正确触发(onSuccess,onFinish),但我的 ProgressBar 一直冻结(甚至尝试在单独的线程中运行它......)。

有人有什么想法吗?

4

1 回答 1

2

终于成功了!我使用AndroidQuery(ListView 示例)进行图像加载和缓存(效果很好)。

首先在活动A中制作静态变量(我的SplashScreen活动)

protected static ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();
protected static ArrayList<JSONObject> items = new ArrayList<JSONObject>();

然后使用 AsyncTask 获取您需要的所有 JSON 信息并填充两个 ArrayList,然后 onPostExecute 使您的其他活动(在我的情况下为 MainWindow)。

    @Override
    protected Integer doInBackground(String[]... params) {



        try
        {
            String address = "http://your.file.here";

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(address);

            try
            {

                HttpResponse response = client.execute(post);
                Log.w("RESPONSE", response.toString());

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;

                while ((sResponse = reader.readLine()) != null) {
                    result = result.append(sResponse);
                }

                reader.close();

                JSONArray news = new JSONArray(result.toString());
                int count = news.length();

                for(int i=0; i<news.length(); i++)
                {
                    JSONObject jo = (JSONObject) news.get(i);
                    joNews.add(jo);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POST_URL", jo.getString("POST_URL"));
                    map.put("TOPIC_TITLE", jo.getString("TOPIC_TITLE"));
                    map.put("AUTHOR", jo.getString("AUTHOR"));
                    map.put("CATEGORY", jo.getString("CATEGORY"));
                    map.put("POST_TIME", jo.getString("POST_TIME"));
                    map.put("POST_TEXT", jo.getString("POST_TEXT"));
                    map.put("IMAGE", jo.getString("IMAGE"));

                    allNews.add(map);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 1;
    }

     protected void onPostExecute(Integer result) {
         Intent main = new Intent(SplashScreen.this, MainWindow.class);
         SplashScreen.this.startActivity(main);
         SplashScreen.this.finish();
     }

最后,只需初始化 Activity B(我的 MainWindow 活动)中的变量,然后使用 AndroidQuery 将它们加载到 ListView 中!

private ArrayList<HashMap<String, String>> allNews = new ArrayList<HashMap<String, String>>();
private ArrayList<JSONObject> items = new ArrayList<JSONObject>();

    allNews = SplashScreen.allNews;
    items = SplashScreen.joNews;

    ArrayAdapter<JSONObject> aa = new ArrayAdapter<JSONObject>(this, R.layout.main_news_items, items){

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

                if(convertView == null){
                        convertView = getLayoutInflater().inflate(R.layout.main_news_items, null);
                }

                JSONObject jo = getItem(position);

                AQuery aq = new AQuery(convertView);
                aq.id(R.id.name).text(jo.optString("TOPIC_TITLE", "No Title"));
                aq.id(R.id.title).text(jo.optString("CATEGORY", ""));

                String tb = jo.optString("IMAGE");
                if(tb.equals(""))
                    tb = "http://default.image.here";
                aq.id(R.id.profile_img).progress(R.id.progress).image(tb, true, true, 0, 0, null, AQuery.FADE_IN_NETWORK, 1.0f);


                return convertView;

        }
    };

    ListView news = (ListView) findViewById(R.id.listView1);
    news.setAdapter(aa);
    news.setTextFilterEnabled(true);

希望这对某人有帮助!

于 2012-12-25T12:37:47.253 回答