php - Error getting Images into Listview from server using Json -
i not getting listview images server...it gives me launcher images not image server...
here submenu activity:
public class submenu extends activity { // declare variables jsonobject jsonobject; jsonarray jsonarray; listview listview; listviewadapter adapter; progressdialog mprogressdialog; arraylist<hashmap<string, string>> arraylist; static string rank = "id"; static string country = "name"; static string flag = "image"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // view listview_main.xml setcontentview(r.layout.activity_sub_menu); // execute downloadjson asynctask new downloadjson().execute(); } // downloadjson asynctask private class downloadjson extends asynctask<void, void, void> { // @override // protected void onpreexecute() { // super.onpreexecute(); // create progressdialog // mprogressdialog = new progressdialog(submenu.this); // set progressdialog title // mprogressdialog.settitle("categories of main categories....."); // set progressdialog message // mprogressdialog.setmessage("loading..."); // mprogressdialog.setindeterminate(false); // show progressdialog // mprogressdialog.show(); // } @override protected void doinbackground(void... params) { // create array arraylist = new arraylist<hashmap<string, string>>(); // retrieve json objects given url address jsonarray = jsonfunctions .getjsonfromurl("http://cloud.granddubai.com/broccoli/menu_typeitem.php"); try { // locate array name in json // jsonarray = jsonobject.getjsonarray("main_menu_items"); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject = jsonarray.getjsonobject(i); // retrive json objects map.put("id", jsonobject.getstring("id")); map.put("name", jsonobject.getstring("name")); map.put("image", jsonobject.getstring("image")); // set json objects array arraylist.add(map); } } catch (jsonexception e) { log.e("error", e.getmessage()); e.printstacktrace(); } return null; } @override protected void onpostexecute(void args) { // locate listview in listview_main.xml listview = (listview) findviewbyid(r.id.list1); // pass results listviewadapter.java adapter = new listviewadapter(submenu.this, arraylist); // set adapter listview listview.setadapter(adapter); // close progressdialog // mprogressdialog.dismiss(); } } }
i new android ...one more thing few images not available in table.is reason???? here json:
[ { "id": "1", "name": "bianca pizza", "image": "http://cloud.granddubai.com/broccoli/uploads/bianca.png" }, { "id": "2", "name": "cheese pizza", "image": "http://cloud.granddubai.com/broccoli/uploads/cheese.png" }, { "id": "3", "name": "chicken bbq pizza:", "image": "http://cloud.granddubai.com/broccoli/uploads/chicken bbq.png" }, { "id": "4", "name": "deluxe pizza", "image": "http://cloud.granddubai.com/broccoli/uploads/deluxe.png" }, { "id": "5", "name": "greek pizza", "image": "http://cloud.granddubai.com/broccoli/uploads/greek.png" }, { "id": "6", "name": "hawaiian pizza", "image": "http://cloud.granddubai.com/broccoli/uploads/hawaiian.png" }, { "id": "7", "name": "meat lovers pizza", "image": "http://cloud.granddubai.com/broccoli/uploads/meat lovers.png" }, { "id": "8", "name": "pepperoni pizza", "image": "" }, { "id": "9", "name": "shrimp pizza", "image": "" }, { "id": "10", "name": "vegetable pizza", "image": "" }, { "id": "11", "name": "original italian pizza", "image": "" }, { "id": "12", "name": "beef strips pasta", "image": "" }, { "id": "13", "name": "chicken pasta", "image": "" }, { "id": "14", "name": "meat balls pasta", "image": "http://cloud.granddubai.com/broccoli/uploads/meat lovers.png" }, { "id": "15", "name": "sausage pasta", "image": "http://cloud.granddubai.com/broccoli/uploads/sausage.png" }, { "id": "16", "name": "shrimp pasta", "image": "http://cloud.granddubai.com/broccoli/uploads/shrimp.png" }, { "id": "17", "name": "vegetable pasta", "image": "http://cloud.granddubai.com/broccoli/uploads/vegetable.png" }, { "id": "18", "name": "pesto pasta", "image": "http://cloud.granddubai.com/broccoli/uploads/pesto.png" }, { "id": "19", "name": "chicken lasagna", "image": "" }, { "id": "20", "name": "meat lasagna", "image": "" }, { "id": "21", "name": "vegetable lasagna", "image": "" }, { "id": "22", "name": "caesar salad", "image": "http://cloud.granddubai.com/broccoli/uploads/caesar.png" }, { "id": "23", "name": "chicken caesar salad", "image": "http://cloud.granddubai.com/broccoli/uploads/chicken caesar.png" }, { "id": "24", "name": "garden salad", "image": "http://cloud.granddubai.com/broccoli/uploads/garden.png" }, { "id": "25", "name": "greek salad", "image": "" }, { "id": "26", "name": "pasta salad", "image": "http://cloud.granddubai.com/broccoli/uploads/pasta salad.png" }, { "id": "27", "name": "sunny side ", "image": "" }, { "id": "28", "name": "omelette ", "image": "" }, { "id": "29", "name": "scrambled eggs", "image": "" }, { "id": "30", "name": "scrambled egg toppings", "image": "" }, { "id": "31", "name": "coffee", "image": "" }, { "id": "32", "name": "fresh juice", "image": "" }, { "id": "36", "name": "water 500ml", "image": "" }, { "id": "35", "name": "pepsi can", "image": "" }, { "id": "37", "name": "soup", "image": "" }, { "id": "38", "name": "special offer family pack", "image": "" } ]
this php file
<?php include ('config.php'); $id = $_get['id']; $sql = mysqli_query($conn,"select * main_menu_items "); $arr = array(); $i=0; while($result = mysqli_fetch_array($sql)) { $arr[$i]['id']= $result['id']; $arr[$i]['name']= $result['name']; $arr[$i]['image']=$result['image']; $i++; } echo json_encode($arr); ?>
this imageloader:
public class imageloader { memorycache memorycache = new memorycache(); filecache filecache; private map<imageview, string> imageviews = collections .synchronizedmap(new weakhashmap<imageview, string>()); executorservice executorservice; // handler display images in ui thread handler handler = new handler(); public imageloader(context context) { filecache = new filecache(context); executorservice = executors.newfixedthreadpool(5); } final int stub_id = r.drawable.nicon1; public void displayimage(string url, imageview imageview) { imageviews.put(imageview, url); bitmap bitmap = memorycache.get(url); if (bitmap != null) imageview.setimagebitmap(bitmap); else { queuephoto(url, imageview); imageview.setimageresource(stub_id); } } private void queuephoto(string url, imageview imageview) { phototoload p = new phototoload(url, imageview); executorservice.submit(new photosloader(p)); } private bitmap getbitmap(string url) { file f = filecache.getfile(url); bitmap b = decodefile(f); if (b != null) return b; // download images internet try { bitmap bitmap = null; url imageurl = new url(url); httpurlconnection conn = (httpurlconnection) imageurl .openconnection(); conn.setconnecttimeout(30000); conn.setreadtimeout(30000); conn.setinstancefollowredirects(true); inputstream = conn.getinputstream(); outputstream os = new fileoutputstream(f); utils.copystream(is, os); os.close(); conn.disconnect(); bitmap = decodefile(f); return bitmap; } catch (throwable ex) { ex.printstacktrace(); if (ex instanceof outofmemoryerror) memorycache.clear(); return null; } } // decodes image , scales reduce memory consumption private bitmap decodefile(file f) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; fileinputstream stream1 = new fileinputstream(f); bitmapfactory.decodestream(stream1, null, o); stream1.close(); // find correct scale value. should power of 2. // recommended size 512 final int required_size = 70; int width_tmp = o.outwidth, height_tmp = o.outheight; int scale = 1; while (true) { if (width_tmp / 2 < required_size || height_tmp / 2 < required_size) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; fileinputstream stream2 = new fileinputstream(f); bitmap bitmap = bitmapfactory.decodestream(stream2, null, o2); stream2.close(); return bitmap; } catch (filenotfoundexception e) { } catch (ioexception e) { e.printstacktrace(); } return null; } // task queue private class phototoload { public string url; public imageview imageview; public phototoload(string u, imageview i) { url = u; imageview = i; } } class photosloader implements runnable { phototoload phototoload; photosloader(phototoload phototoload) { this.phototoload = phototoload; } @override public void run() { try { if (imageviewreused(phototoload)) return; bitmap bmp = getbitmap(phototoload.url); memorycache.put(phototoload.url, bmp); if (imageviewreused(phototoload)) return; bitmapdisplayer bd = new bitmapdisplayer(bmp, phototoload); handler.post(bd); } catch (throwable th) { th.printstacktrace(); } } } boolean imageviewreused(phototoload phototoload) { string tag = imageviews.get(phototoload.imageview); if (tag == null || !tag.equals(phototoload.url)) return true; return false; } // used display bitmap in ui thread class bitmapdisplayer implements runnable { bitmap bitmap; phototoload phototoload; public bitmapdisplayer(bitmap b, phototoload p) { bitmap = b; phototoload = p; } public void run() { if (imageviewreused(phototoload)) return; if (bitmap != null) phototoload.imageview.setimagebitmap(bitmap); else phototoload.imageview.setimageresource(stub_id); } } public void clearcache() { memorycache.clear(); filecache.clear(); } }
here listview adapter
public class listviewadapter extends baseadapter { // declare variables context context; layoutinflater inflater; arraylist<hashmap<string, string>> data; imageloader imageloader; hashmap<string, string> resultp = new hashmap<string, string>(); public listviewadapter(context context, arraylist<hashmap<string, string>> arraylist) { this.context = context; data = arraylist; imageloader = new imageloader(context); } @override public int getcount() { return data.size(); } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } public view getview(final int position, view convertview, viewgroup parent) { // declare variables textview id; textview name; textview population; imageview image; inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view itemview = inflater.inflate(r.layout.list_item1, parent, false); // position resultp = data.get(position); // locate textviews in listview_item.xml id = (textview) itemview.findviewbyid(r.id.idq); name = (textview) itemview.findviewbyid(r.id.type1); // locate imageview in listview_item.xml image = (imageview) itemview.findviewbyid(r.id.subimg); // capture position , set results textviews id.settext(resultp.get(submenu.rank)); name.settext(resultp.get(submenu.country)); // capture position , set results imageview // passes flag images url imageloader.class imageloader.displayimage(resultp.get(submenu.flag), image); // capture listview item click /**itemview.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // position resultp = data.get(position); intent intent = new intent(context, singleitemview.class); // pass data rank intent.putextra("rank", resultp.get(mainactivity.rank)); // pass data country intent.putextra("country", resultp.get(mainactivity.country)); // pass data population intent.putextra("population",resultp.get(mainactivity.population)); // pass data flag intent.putextra("flag", resultp.get(mainactivity.flag)); // start singleitemview class context.startactivity(intent); } });*/ return itemview; } }
you need use this:
jsonarray response = jsonfunctions .getjsonfromurl("http://cloud.gr....../broccoli/menu_typeitem.php");
because structure of response server jsonarray([]) not jsonobject({}). starting element [] not {}.
add below method in jsonfunctions class , call method:
public static jsonarray getjsonfromurl(string url) { inputstream = null; string result = ""; jsonarray jarray = null; // download json data url try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection " + e.tostring()); } // convert response string try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } try { jarray = new jsonarray(result); } catch (jsonexception e) { log.e("log_tag", "error parsing data " + e.tostring()); } return jarray; }
i changed code, please use it:
public class submenu extends activity { // declare variables jsonobject jsonobject; jsonarray jsonarray; listview listview; listviewadapter adapter; progressdialog mprogressdialog; arraylist<hashmap<string, string>> arraylist; static string rank = "id"; static string country = "name"; static string flag = "image"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // view listview_main.xml setcontentview(r.layout.activity_sub_menu); // execute downloadjson asynctask new downloadjson().execute(); } // downloadjson asynctask private class downloadjson extends asynctask<void, void, void> { // @override // protected void onpreexecute() { // super.onpreexecute(); // create progressdialog // mprogressdialog = new progressdialog(submenu.this); // set progressdialog title // mprogressdialog.settitle("categories of main categories....."); // set progressdialog message // mprogressdialog.setmessage("loading..."); // mprogressdialog.setindeterminate(false); // show progressdialog // mprogressdialog.show(); // } @override protected void doinbackground(void... params) { // create array arraylist = new arraylist<hashmap<string, string>>(); // retrieve json objects given url address jsonarray = jsonfunctions .getjsonfromurl("http://cloud.gr....../broccoli/menu_typeitem.php"); try { // locate array name in json // jsonarray = jsonobject.getjsonarray("main_menu_items"); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject = jsonarray.getjsonobject(i); // retrive json objects map.put("id", jsonobject.getstring("id")); map.put("name", jsonobject.getstring("name")); map.put("image", jsonobject.getstring("image")); // set json objects array arraylist.add(map); } } catch (jsonexception e) { log.e("error", e.getmessage()); e.printstacktrace(); } return null; } @override protected void onpostexecute(void args) { // locate listview in listview_main.xml listview = (listview) findviewbyid(r.id.list1); // pass results listviewadapter.java adapter = new listviewadapter(submenu.this, arraylist); // set adapter listview listview.setadapter(adapter); // close progressdialog mprogressdialog.dismiss(); } } }
as there no key named "main_menu_items", commented out code.
Comments
Post a Comment