EventHome.java的AsyncTask
class LoadProfile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EventHome.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Profile JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
String json = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PROFILE_URL);
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
json = EntityUtils.toString(resEntity);
Log.i("All Events: ", json.toString());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
// dismiss the dialog after getting all products
pDialog.dismiss();
try{
event_all = new JSONObject(json);
JSONArray user = event_all.getJSONArray("events");
JSONObject jb= user.getJSONObject(0);
String name = jb.getString("name");
String venue=jb.getString("location");
String date=jb.getString("date_d");
String descr=jb.getString("descr");
String image1=jb.getString("images1");
// displaying all data in textview
tv3.setText(name);
tv4.setText(venue+", "+date);
tv5.setText(descr);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
json输出字符串
{"events":[{"id":"1","name":"Demo Event","descr":"This is a demo","date_d":"12\/27\/2014","time_t":"","images":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo.png","images1":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo1.png","location":"Ernakulam","publish":"1"},{"id":"2","name":"Flower Show","descr":"xfvslkdfvsw fvgersdgvel fgvmerlkgjer fgertgkjerltgf dfrgergt","date_d":"12\/29\/2014","time_t":"","images":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo.png","images1":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo1.png","location":"Thodupuzha","publish":"1"},{"id":"3","name":"Cultural Event","descr":"sfsdnfkjn sdfrelegkrg fdgvlkgvjmer fdgbelkrek dferlkgelrk dfgerge","date_d":"12\/30\/2014","time_t":"","images":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo.png","images1":"http:\/\/www.adagioinfotech.com\/event\/uploads\/event\/demo1.png","location":"Idukki","publish":"1"}]}
我想显示json值的图像。我怎么能这样做。
答案 0 :(得分:1)
尝试使用picasso
这样加载图片
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
// dismiss the dialog after getting all products
pDialog.dismiss();
try{
event_all = new JSONObject(json);
JSONArray user = event_all.getJSONArray("events");
JSONObject jb= user.getJSONObject(0);
String name = jb.getString("name");
String venue=jb.getString("location");
String date=jb.getString("date_d");
String descr=jb.getString("descr");
String image1=jb.getString("images1");
// displaying all data in textview
tv3.setText(name);
tv4.setText(venue+", "+date);
tv5.setText(descr);
if(image1 != null && !image1.equalsIgnoreCase(""))
Picasso.with(EventHome.this).load(image1).into(iv7);
}catch(Exception e)
{
e.printStackTrace();
}
}
将picasso jar放在项目的libs
文件夹中,或将其添加到build.gradle文件中的依赖项中(参见Picasso site)
答案 1 :(得分:0)
试试这个
protected void onPostExecute(String json) { .....
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(fallback)
.showImageOnFail(fallback)
.showImageOnLoading(fallback).build();
//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView1)
//download and display image from url
imageLoader.displayImage(image1, imageView, options);