java - To display Images from JSON parsed URL in Gallery for an Android application, But gives MalformedURL exception -
i tried solve malformed url exception, didnt work. searched posts relating malformed exception in android problem still persists. works when skip json parsing , declares imagearray in same java class imageid[]= {"http://google.com/images/ranipuram.jpg","http://google.com/images/ranipuram.jpg",}; url when splitted , printed using system.out.println(imageids[0]); not working url.
this code:
public class galleryactivity extends activity { private static final string key_root = districtactivity.dist; static final string key_id = "id"; public string key_image= "image_url"; public string[] imageids; jsonarray contacts; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.gallery_activity); string newstring = districtactivity.dist; final string url = "http://192.168.0.105/tour/"+newstring+".php"; jsonparser jparser = new jsonparser(); // getting json string url jsonobject json = jparser.getjsonfromurl(url); try { // getting array of contacts contacts = json.getjsonarray(key_root); // looping through contacts (int = 0; < contacts.length(); i++) { jsonobject c = contacts.getjsonobject(i); string image_url =c.getstring(key_image); if(customizedlistview.pos==i) { imageids=image_url.split(","); /* system.out.println(imageids[0]); */ } }// storing each json item in variable } catch (jsonexception e) { e.printstacktrace(); } gallery gallery = (gallery) findviewbyid(r.id.gallery1); gallery.setadapter(new imageadapter(this)); gallery.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview parent, view v, int position, long id) { // toast.maketext(getbasecontext(), "pic" + (position + 1) + // " selected", toast.length_short).show(); // ---display images selected--- imageview imageview = (imageview) findviewbyid(r.id.imageview1); url url = null; try { url = new url(imageids[position].tostring()); bitmap bmp = null; try { bmp = bitmapfactory.decodestream(url .openconnection().getinputstream()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } imageview.setimagebitmap(bmp); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }); } public class imageadapter extends baseadapter { private context context; private int itembackground; public imageadapter(context c) { context = c; // ---setting style--- typedarray = obtainstyledattributes(r.styleable.gallery1); itembackground = a.getresourceid( r.styleable.gallery1_android_galleryitembackground, 0); a.recycle(); } // ---returns number of images--- @override public int getcount() { return imageids.length; } // ---returns id of item--- @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return position; } // ---returns imageview view--- @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview = new imageview(context); // imageview.setimageresource(imageids[position]); url url; try { url = new url (imageids[position]); bitmap bmp = bitmapfactory.decodestream(url.openconnection() .getinputstream()); imageview.setimagebitmap(bmp); imageview.setscaletype(imageview.scaletype.fit_xy); imageview.setlayoutparams(new gallery.layoutparams(150, 120)); imageview.setbackgroundresource(itembackground); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return imageview; } } }
// json- >
the url's given in table , column name "image_url", datatype "text". , when tried display them in textview in emulator , displays same given in database.
//resolved problem
the quotes made problem !there no need double quotes url's in database. removed double quotes , working. amulya khare .
Comments
Post a Comment