我从URL获取JSON响应并将其转换为字符串。我从响应中获取了一个图像的URL。我想从URL下载该图像并将其显示在ListView中。但是这需要花费很多时间,并且在下载所有图像之前显示空白屏幕。屏幕显示将近40-50秒。 ListView也不是很流畅。如何防止出现空白屏幕?
这是我的代码: -
String registerContet = "myUrl";
String items;
try
{
items = new FetchItems().execute(registerContet).get();
pDialog = new ProgressDialog(this).show(Home.this, "Fetching news items", "Please wait..");
JSONArray jObject = new JSONArray(items);
for (int i = 0; i < jObject.length(); i++)
{
JSONObject menuObject = jObject.getJSONObject(i);
String title= menuObject.getString("Title");
String description= menuObject.getString("BodyText");
String thumbnail= menuObject.getString("ThumbnailPath");
String newsUrl = menuObject.getString("Url");
String body = menuObject.getString("Body");
String newsBigImage = menuObject.getString("ImageBlobUrls");
map = new HashMap<String,String>();
map.put(SOURCETITLE, title);
map.put(TITLE, description);
map.put(THUMBNAILPATH, thumbnail);
map.put(BODY, body);
map.put(URL, newsUrl);
map.put(IMAGEBLOBURLS,newsBImage);
myNList.add(map);
}
itemsAdapter = new LazyAdapter(Home.this, myNList);
if(pDialog!=null && pDialog.isShowing())
{
pDialog.dismiss();
}
nList.setAdapter(itemsAdapter);
nList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
myDialog = new ProgressDialog(Home.this).show(Home.this, "Fetching news..", "Just a moment");
HashMap<String, String> myMap = myNList.get(position);
Intent nIntent = new Intent(Home.this,NDetails.class);
newsIntent.putExtra("NItems", myMap);
startActivity(nIntent);
}
});
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class FetchItems extends AsyncTask<String, String, String>
{
// TODO Auto-generated method stub
ProgressDialog myDialog;
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
HttpResponse response =null;
String resultString = "";
String myResponseBody = "" ;
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpGet request = new HttpGet(params[0]);
try
{
response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()== 200)
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = entity.getContent();
myResponseBody = convertToString(inputStream);
}
}
}
catch(Exception e)
{
}
return myResponseBody;
}
@Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
/*if(myDialog.isShowing())
{
myDialog.dismiss();
}*/
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
/*myDialog = new ProgressDialog(Home.this);
myDialog.setMessage("Loading");
myDialog.show();*/
}
任何人都可以告诉我如何解决这个问题。 感谢
答案 0 :(得分:0)
使用Universal Image Loader
,它会缓存您的图片并以多种方式为您提供帮助。
以下是功能:
这是link。你可以进一步阅读,它很容易实现。
答案 1 :(得分:0)
您应该使用单独的子线程从服务器加载图像。 使用通用Image Loader加载图像和保存图像缓存的最佳和有效方法。
load.displayImage(mimagebURL(), holder.image,option);
使用此库,您无需在线程上创建加载图像。
希望这会对你有所帮助