我正在使用j的async lib进行post / get(尝试使用我自己的,同样的事情发生)并且我一直遇到问题。当我调用我的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一直在冻结(甚至尝试在单独的线程中运行它......)。
有人有任何想法吗?
答案 0 :(得分: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信息并填充两个ArrayLists,并使用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();
}
最后,只需初始化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);
希望这有助于某人!