Blank screen while fetching JSON data from url android -
i fetching json response url. url returns huge json response. while response application shows blank(black) screen. want show progressdialog
while data loads. unable think can place dialog box.
here code:
jsonarray mydataitems = null; arraylist<hashmap<string, string>> mydatalist = new arraylist<hashmap<string,string>>(); textview txttitle; textview txtbody; imageview imgthumbnail; hashmap<string, string> map ; //url json array private static string url = "someurl"; progressdialog pdialog; jsonarray newsarray = null; private listadapter itemsadapter; //details detail = new details(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.home); txttitle = (textview)findviewbyid(r.id.title); txtbody = (textview)findviewbyid(r.id.desc); imgthumbnail = (imageview)findviewbyid(r.id.icon); newslist = (listview)findviewbyid(android.r.id.list); mydatalist = new arraylist<hashmap<string, string>>(); usersname = (textview)findviewbyid(r.id.txtusersname); intent in = getintent(); hosturl = in.getstringextra("hosturl"); encodedaccountname = in.getstringextra("encodedaccountname"); refreshtoken = in.getstringextra("refreshtoken"); realm = in.getstringextra("realm"); usersname.settext(in.getstringextra("usersname")); list<rowitem> rowitems; pdialog = new progressdialog(this); string registercontet = "someotherurl"; string items; try { items = new fetchitems().execute(registercontet).get(); jsonarray jobject = new jsonarray(items); (int = 0; < 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,newsbigimage); mynewslist.add(map); itemsadapter = new lazyadapter(home.this, mynewslist); newslist.setadapter(itemsadapter); newslist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3) { // todo auto-generated method stub hashmap<string, string> mymap = mydatalist.get(position); intent nintent = new intent(home.this,ndetails.class); nintent.putextra("items", 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 @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(pdialog.isshowing()) { pdialog.dismiss(); } } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); pdialog.setmessage("loading"); pdialog.show(); } }
lazyadapter.java
public class lazyadapter extends baseadapter { private activity activity; private arraylist<hashmap<string, string>> data; private static layoutinflater inflater = null; public context mycontext; public imageloader imageloader; public context mcontext; public progressdialog pdialog; public lazyadapter(context ctx, arraylist<hashmap<string, string>> d) { mcontext = ctx; data=d; inflater =layoutinflater.from(ctx); //imageloader=new imageloader(); } @override public int getcount() { // todo auto-generated method stub return data.size(); } @override public object getitem(int position) { // todo auto-generated method stub return position; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public boolean isenabled(int position) { // todo auto-generated method stub return true; } @override public view getview(int position, view convertview, viewgroup parent) { view vi=convertview; if(convertview==null) vi = inflater.inflate(com.zevenpooja.attini.r.layout.list_row,null); textview title = (textview)vi.findviewbyid(com.zevenpooja.attini.r.id.title); // title textview description = (textview)vi.findviewbyid(r.id.description); // artist name hashmap<string, string> song = new hashmap<string, string>(); song = data.get(position); // setting values in listview title.settext(song.get(home.sourcetitle)); string mydescription = song.get(home.title); mydescription = mydescription.substring(0, math.min(mydescription.length(), 50)); description.settext(mydescription); string bitmapurl = song.get(home.thumbnailpath); bitmap bitmap = downloadimage(song.get(home.thumbnailpath)); imageview thumb_image=(imageview)vi.findviewbyid(r.id.list_image); // thumb image if(bitmap!=null) { thumb_image.setimagebitmap(bitmap); } else thumb_image = (imageview)vi.findviewbyid(r.id.list_image); // imageloader imgloader = new imageloader(getapplicationcontext()); // imageloader.displayimage(song.get(home.thumbnailpath), thumb_image); return vi; } private class openhttpconnection extends asynctask<string, string, inputstream> { @override protected inputstream doinbackground(string... params) { // todo auto-generated method stub inputstream in = null; int response = -1; try { url url = new url(params[0]); urlconnection conn = url.openconnection(); if (!(conn instanceof httpurlconnection)) throw new ioexception("not http connection"); httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); if (response == httpurlconnection.http_ok) { in = httpconn.getinputstream(); } } catch (exception ex) { try { throw new ioexception("error connecting"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return in; } } private bitmap downloadimage(string url) { bitmap bitmap = null; inputstream in = null; try { in = new openhttpconnection().execute(url).get(); bitmap = bitmapfactory.decodestream(in); in.close(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } return bitmap; } }
how can resolve this? thanks
change fetchitem class :
private class fetchitems extends asynctask<string, string, string> { // todo auto-generated method stub progressdialog dialog; @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); dialog = new progressdialog(context); dialog.setmessage("loading..."); dialog.setcanceledontouchoutside(false); dialog.show(); } @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(void result) { // todo auto-generated method stub super.onpostexecute(result); dialog.dismiss(); } }
Comments
Post a Comment