android - java.net.MalformedURLException: Protocol not found: null -
this mainactivity.java
/** asynctask parse json data , load listview */ private class listviewloadertask extends asynctask<string, void, simpleadapter>{ jsonobject jobject; // doing parsing of xml data in non-ui thread @override protected simpleadapter doinbackground(string... strjson) { try{ jobject = new jsonobject(strjson[0]); storejsonparser countryjsonparser = new storejsonparser(); countryjsonparser.parse(jobject); }catch(exception e){ log.d("json exception1",e.tostring()); } // instantiating json parser class storejsonparser countryjsonparser = new storejsonparser(); // list object store parsed countries list list<hashmap<string, object>> countries = null; try{ // getting parsed data list construct countries = countryjsonparser.parse(jobject); }catch(exception e){ log.d("exception",e.tostring()); } // keys used in hashmap string[] = { "country","flag","details"}; // ids of views in listview_layout int[] = { r.id.storename,r.id.icon}; // instantiating adapter store each items // r.layout.listview_layout defines layout of each item simpleadapter adapter = new simpleadapter(getbasecontext(), countries, r.layout.restaurant_list, from, to); return adapter; } /** invoked android on "doinbackground" executed */ @override protected void onpostexecute(simpleadapter adapter) { // setting adapter listview mlistview.setadapter(adapter); for(int i=0;i<adapter.getcount();i++){ hashmap<string, object> hm = (hashmap<string, object>) adapter.getitem(i); string imgurl = (string) hm.get("flag_path"); imageloadertask imageloadertask = new imageloadertask(); hashmap<string, object> hmdownload = new hashmap<string, object>(); hm.put("flag_path",imgurl); hm.put("position", i); // starting imageloadertask download , populate image in listview imageloadertask.execute(hm); } } } /** asynctask download , load image in listview */ private class imageloadertask extends asynctask<hashmap<string, object>, void, hashmap<string, object>>{ @override protected hashmap<string, object> doinbackground(hashmap<string, object>... hm) { inputstream istream=null; string imgurl = (string) hm[0].get("flag_path"); int position = (integer) hm[0].get("position"); url url; try { url = new url(imgurl); // creating http connection communicate url httpurlconnection urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading data url istream = urlconnection.getinputstream(); // getting caching directory file cachedirectory = getbasecontext().getcachedir(); // temporary file store downloaded image file tmpfile = new file(cachedirectory.getpath() + "/wpta_"+position+".png"); // fileoutputstream temporary file fileoutputstream foutstream = new fileoutputstream(tmpfile); // creating bitmap downloaded inputstream bitmap b = bitmapfactory.decodestream(istream); // writing bitmap temporary file png file b.compress(bitmap.compressformat.png,100, foutstream); // flush fileoutputstream foutstream.flush(); //close fileoutputstream foutstream.close(); // create hashmap object store image path , position in listview hashmap<string, object> hmbitmap = new hashmap<string, object>(); // storing path temporary image file hmbitmap.put("flag",tmpfile.getpath()); // storing position of image in listview hmbitmap.put("position",position); // returning hashmap object containing image path , position return hmbitmap; }catch (exception e) { e.printstacktrace(); } return null; }
this jsonparserclass.java
public class storejsonparser { // receives jsonobject , returns list public list<hashmap<string,object>> parse(jsonobject jobject){ jsonarray jcountries = null; try { // retrieves elements in 'countries' array jcountries = jobject.getjsonarray("storelistfood"); } catch (jsonexception e) { e.printstacktrace(); } // invoking getcountries array of json object // each json object represent country return getcountries(jcountries); } private list<hashmap<string, object>> getcountries(jsonarray jcountries){ int countrycount = jcountries.length(); list<hashmap<string, object>> countrylist = new arraylist<hashmap<string,object>>(); hashmap<string, object> country = null; // taking each country, parses , adds list object for(int i=0; i<countrycount;i++){ try { // call getcountry country json object parse country country = getcountry((jsonobject)jcountries.get(i)); countrylist.add(country); } catch (jsonexception e) { e.printstacktrace(); } } return countrylist; } // parsing country json object private hashmap<string, object> getcountry(jsonobject jcountry){ hashmap<string, object> country = new hashmap<string, object>(); string countryname = ""; string flag=""; /*string language = ""; string capital = ""; string currencycode = ""; string currencyname = "";*/ try { countryname = jcountry.getstring("storename"); flag = jcountry.getstring("logo"); /*language = jcountry.getstring("language"); capital = jcountry.getstring("capital"); currencycode = jcountry.getjsonobject("currency").getstring("code"); currencyname = jcountry.getjsonobject("currency").getstring("currencyname");*/ /*string details = "language : " + language + "\n"; + "capital : " + capital + "\n" + "currency : " + currencyname + "(" + currencycode + ")";*/ country.put("country", countryname); country.put("flag", r.drawable.ninety_six); country.put("flag_path", flag); //country.put("details", details); } catch (jsonexception e) { e.printstacktrace(); } return country; } }
this error logcat
java.net.malformedurlexception: protocol not found: null
at line url = new url(imgurl);
, private class imageloadertask
i trying load images in listview using json. pretty sure url in database valid. http://wptrafficanalyzer.in//p//demo1//india.png
Comments
Post a Comment