我正在使用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保持冻结(甚至尝试在单独的线程中运行它)。
任何人有任何想法吗?
最佳答案
终于成功了!我使用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进行您的其他Activity(在我的情况下为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();
}
最后,只需在活动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);
希望这对某人有帮助!