php - How to converting url to image in HashMap? -
this json
encoded code want want show image , title both in android
not able show images
{"lists": [{"post_title":"kigs", "post_img":"post_img":"http://truzzinfotech.com/wp-content/uploads/2014/04/19-150x150.jpg"}, {"post_title":"lacolline", "post_img":"http://truzzinfotech.com/wp-content/uploads/2014/04/19-150x150.jpg"}, {"post_title":"cricket", "post_img":"http://truzzinfotech.com/wp-content/uploads/2014/047-150x150.jpg"}, ], "success":1}
and android code getting images , title
public class portfolio extends listactivity{
// progress dialog private progressdialog pdialog; imageview image; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> portfolioslist; // url products list private static string url_all_portfolios = "http://truzzinfotech.com/wp-content/themes/truzz/getportfolio.php"; // json node names private static final string tag_success = "success"; private static final string tag_portfolios = "lists"; static final string tag_title = "post_title"; static final string tag_image = "post_img"; // products jsonarray jsonarray lists = null; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.portfolio); //listview lv = getlistview(); // hashmap listview portfolioslist = new arraylist<hashmap<string, string>>(); // loading products in background thread new loadallportfolios().execute(); image = (imageview)findviewbyid(r.id.portimg); } /** * background sync task load product making http request * */ class loadallportfolios extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(portfolio.this); pdialog.setmessage("loading portfolio. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); //toast.maketext(getbasecontext(), "enter in loading" ,toast.length_long).show(); } /** * getting products url * */ protected string doinbackground(string... args) { // building parameters //toast.maketext(getbasecontext(), "enter in doinbackground" ,toast.length_long).show(); list<namevaluepair> params = new arraylist<namevaluepair>(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_portfolios,"get", params); log.d("all products: ", json.tostring()); try { //lists = new jsonarray(tag_portfolios); int success = json.getint(tag_success); if (success == 1) { // portfolios found // getting array of portfolios lists = json.getjsonarray(tag_portfolios); // c = null; // looping through products (int = 0; < lists.length(); i++) { jsonobject c = lists.getjsonobject(i); string title = c.getstring(tag_title); string image = c.getstring(tag_image); log.d("item name: ",title); hashmap<string, string> map = new hashmap<string, string>(); url url = new url(image); httpurlconnection con = (httpurlconnection) url.openconnection(); con.setdoinput(true); con.connect(); inputstream input = con.getinputstream(); bitmap bitmap = bitmapfactory.decodestream(input); //inputstream in = new url(tag_image).openstream(); //bmp = bitmapfactory.decodestream(in); map.put(tag_title, title); map.put(tag_image, image); //map.put("image", jsonobject.getstring("image")); portfolioslist.add(map); } } else { // no products found // launch add new product activity toast.maketext(getbasecontext(), "no portfolio found" ,toast.length_long).show(); } } catch (jsonexception e) { // todo auto-generated catch block toast.maketext(getbasecontext(), "no data found" ,toast.length_long).show(); e.printstacktrace(); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( portfolio.this, portfolioslist, r.layout.list_item, new string[] { tag_title,tag_image }, new int[] {r.id.name, r.id.portimg}); // updating listview setlistadapter(adapter); } }); } } }
your can use volley library google of dirty stuff 1 has handle during networking
com.android.volley.toolbox.networkimageview android:id="@+id/networkimageview" android:layout_width="150dp" android:layout_height="170dp" android:layout_centerhorizontal="true" />
networkimageview if image view part of list view manages well
imageloader mimageloader; networkimageview mnetworkimageview; private static final string image_url = "http://developer.android.com/images/training/system-ui.png"; ... // networkimageview display image. mnetworkimageview = (networkimageview) findviewbyid(r.id.networkimageview); // imageloader through singleton class. // mimagaloader request queue of manages caching // , requesting image network in not present in cache mimageloader = mysingleton.getinstance(this).getimageloader(); // set url of image should loaded view, , // specify imageloader used make request. mnetworkimageview.setimageurl(image_url, mimageloader);
reference : https://developer.android.com/training/volley/request.html
mimageloader reference : https://developer.android.com/training/volley/requestqueue.html#singleton how setup singleton class , why
Comments
Post a Comment