2012-12-24 52 views
1

我使用j的異步lib post/get(嘗試與我自己的,同樣的事情發生),我不斷遇到問題。我調用getNews()方法時,ProgressBar(不確定)會一直凍結。我在這裏做錯了什麼?j的異步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保持凍結(甚至嘗試在單獨的線程中運行它...)。

任何人有任何想法?

+1

你試過這個使用asynctask? –

+0

是的,正如我在括號中所說的,我嘗試了我自己的方式(通過類擴展AsyncTask)。 –

+0

當你使用AsyncTask時你遇到了什麼問題?如果你使用AsyncTask獲取實現當前代碼的任何錯誤,那麼PLZ共享錯誤日誌,並且還集成了AsyncTask。 –

回答

2

終於成功了!我用AndroidQuery(ListView示例)進行圖像加載和緩存(效果很好)。

首先,請在活動的靜態變量(我的閃屏活動)

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

然後用的AsyncTask採取一切您需要的JSON信息,並填寫雙方的ArrayList和onPostExecute讓你的其他活動(主窗口在我的情況)。最後,只需在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); 

希望這可以幫助別人!