java - AsyncTask Error when Network is not Stable -
i still new android development. wanna ask asynctask.
i developing android apps using fragments, achartengine, , asynctask parse json file server. if connection stable , runs well, fine. fragment parse json file according url have selected.
however, have problem when connection not stable. how produce problem.
- i connected access point.
- i ran apps. fine. changed landscape portrait landscape , vice versa. moved between fragment. there no problem.
- whereas, when tried disconnect ap. problem started. moved fragment did't show exception prepared. showed previous graph created when connection fine.
- this error talking.
- i want load graph using url everytime fragment started.
this how generate fragment.
private void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new homefragment(); break; case 1: fragment = new backbonefragment(); break; case 2: fragment = new minyakfragment(); break; case 3: fragment = new gasfragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(navmenutitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } }
this fragment class
public class gasfragment extends fragment { //development url private static string url = "http://192.168.0.100/jsontraining/flash_data_url_gas_die.json"; //y-values private static arraylist<hashmap<string, arraylist<long>>> jsonlist = new arraylist<hashmap<string, arraylist<long>>>(); private static hashmap<string, arraylist<long>> mapvalues = new hashmap<string, arraylist<long>>(); //legends private static arraylist<hashmap<string, string>> jsonliststring = new arraylist<hashmap<string, string>>(); private static hashmap<string, string> mapvaluestext = new hashmap<string,string>(); private static arraylist<string> datestringarray = new arraylist<string>(); private graphicalview chartview; public gasfragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_gas, container, false); new jsonparse().execute(); return rootview; } @suppresswarnings("unchecked") @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } @override public void onresume() { super.onresume(); jsonlist.clear(); mapvalues.clear(); } private class jsonparse extends asynctask<string, string, jsonobject> { private progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity()); pdialog.setmessage("getting data ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); }//end of onpreexecute @override protected jsonobject doinbackground(string... args) { jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); return json; }//end of doinbackground @override protected void onpostexecute(jsonobject json) { pdialog.dismiss(); try { //fetching array of elements jsonarray jsonarray = json.getjsonarray("elements"); //fetching textview //fetching pertamina ep jsonobject jsonyaxis = json.getjsonobject("y_axis"); long yaxismin = jsonyaxis.getlong("min"); long yaxismax = jsonyaxis.getlong("max"); jsonobject jsonylegend = json.getjsonobject("y_legend"); string y_legend = jsonylegend.getstring("text"); jsonobject jsonxlegend = json.getjsonobject("x_legend"); string x_legend = jsonxlegend.getstring("text"); textview uid; jsonobject jsontitle= json.getjsonobject("title"); string title = jsontitle.getstring("text"); uid = (textview) getactivity().findviewbyid(r.id.txtlabel_gas); uid.settext(title); //fetching date jsonobject jsonobjectxaxis = json.getjsonobject("x_axis"); jsonobject jsonobjectxaxislabels = jsonobjectxaxis.getjsonobject("labels"); jsonarray jsonarrayxaxislabels = jsonobjectxaxislabels.getjsonarray("labels"); for(int i=0;i<jsonarrayxaxislabels.length();i++) { datestringarray.add((string) jsonarrayxaxislabels.getstring(i)); } (int i=0;i<jsonarray.length();i++) { arraylist<long> list = new arraylist<long>(); hashmap<string, arraylist<long>> mapvalues = new hashmap<string, arraylist<long>>(); hashmap<string, string> mapvaluestext = new hashmap<string,string>(); jsonobject elementno = (jsonobject) jsonarray.get(i); jsonarray valuesgraph = (jsonarray) elementno.get("values"); string texts = elementno.getstring("text"); string colours = elementno.getstring("colour"); int len = valuesgraph.length(); (int x=0;x<len;x++) { if(!valuesgraph.isnull(x)) { long nilai = (long)valuesgraph.getlong(x); list.add(nilai); } } mapvalues.put("values", list); mapvaluestext.put("text", texts); mapvaluestext.put("colour", colours); jsonlist.add(mapvalues); jsonliststring.add(mapvaluestext); } //line graph xymultipleseriesdataset dataset = new xymultipleseriesdataset(); xymultipleseriesrenderer mrenderer = new xymultipleseriesrenderer(); mrenderer.setapplybackgroundcolor(true); mrenderer.setbackgroundcolor(color.rgb(254, 254, 254)); mrenderer.setmarginscolor(color.rgb(254, 254, 254)); int[] x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; for(int start=0; start<jsonlist.size(); start++) { //start++; mapvaluestext = jsonliststring.get(start); timeseries series = new timeseries(mapvaluestext.get("text").trim()+""); //start--; mapvalues = jsonlist.get(start); int jumlahvalues = mapvalues.get("values").size(); for(int i=0; i<jumlahvalues; i++) { series.add(x[i], mapvalues.get("values").get(i)); } dataset.addseries(series); xyseriesrenderer renderer = new xyseriesrenderer(); renderer.setshowlegenditem(true); renderer.setchartvaluestextsize(24); renderer.setcolor(color.parsecolor(mapvaluestext.get("colour").tostring())); renderer.setlinewidth(3); renderer.setpointstyle(pointstyle.circle); renderer.setfillpoints(true); //renderer.setdisplaychartvalues(true); mrenderer.addseriesrenderer(renderer); } //xy titles mrenderer.setytitle(y_legend); //mrenderer.setxtitle(x_legend); mrenderer.setcharttitle(x_legend); mrenderer.setcharttitletextsize(30); mrenderer.setaxistitletextsize(22); mrenderer.setlabelstextsize(22); mrenderer.setxlabelscolor(color.black); mrenderer.setylabelscolor(0, color.black); mrenderer.setshowlabels(true); mrenderer.setxlabels(datestringarray.size()); //legends //legends mrenderer.setshowlegend(true); //mrenderer.setfitlegend(true); mrenderer.setlegendtextsize(20); mrenderer.setxlabelspadding(20); mrenderer.setylabelsalign(align.right, 0); mrenderer.setylabelspadding(7); mrenderer.setylabelsverticalpadding(-5); if (getresources().getconfiguration().orientation == configuration.orientation_portrait) { (int = 0; < jsonarrayxaxislabels.length();) { string xchartdates = jsonarrayxaxislabels.getstring(i); mrenderer.addtextlabel(i+1, xchartdates); if(mapvalues.get("values").size()<9) { i=i+1; } else { i=i+4; } } mrenderer.setxlabelsalign(align.center); mrenderer.setxlabels(0); } else { (int = 0; < jsonarrayxaxislabels.length(); i++) { string xchartdates = jsonarrayxaxislabels.getstring(i); mrenderer.addtextlabel(i+1, xchartdates); } mrenderer.setxlabelsalign(align.center); mrenderer.setxlabels(0); } mrenderer.setaxescolor(color.black); mrenderer.setlabelscolor(color.black); mrenderer.setmargins(new int[]{ 10, 110, 10, 10}); mrenderer.setyaxismax(yaxismax); mrenderer.setyaxismin(yaxismin); mrenderer.setpointsize(9.5f); //mrenderer.setpanlimits(new double[]{0, (jsonarrayxaxislabels.length())*1.1, yaxismin, yaxismax}); mrenderer.setinscroll(true); mrenderer.setclickenabled(true); //grid mrenderer.setshowgrid(true); //mrenderer.setshowgridx(true); //mrenderer.setshowgridy(true); mrenderer.setshowcustomtextgrid(true); mrenderer.setgridcolor(color.rgb(162,172,186)); if(chartview!=null) { chartview.repaint(); } else{ chartview = chartfactory.getlinechartview(getactivity(), dataset, mrenderer); //chartview.repaint(); //layout.addview(chartview, new layoutparams(960,layoutparams.fill_parent)); } chartview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { seriesselection seriesselection = chartview.getcurrentseriesandpoint(); double[] xy = chartview.torealpoint(0); if (seriesselection == null) { //toast.maketext(getactivity(), "no chart element clicked", toast.length_short).show(); } else { long values = (long) seriesselection.getvalue(); mapvaluestext = jsonliststring.get(seriesselection.getseriesindex()); int xvalue = (int)seriesselection.getxvalue()-1; toast.maketext(getactivity(), ""+mapvaluestext.get("text").trim()+". \n"+numberformat.getnumberinstance(locale.german).format(values) +" bbls pada hari ke-"+datestringarray.get(xvalue)+"" , toast.length_long).show(); } } }); linearlayout layout = (linearlayout) getactivity().findviewbyid(r.id.dashboard_chart_layout_gas); layout.removeallviews(); layout.addview(chartview); } catch (jsonexception e) { e.printstacktrace(); } }//end of doinbackground }}
here fragment class
public class homefragment extends fragment { //development url private static string url = "http://192.168.0.100/jsontraining/production.json"; //y-values private static arraylist<hashmap<string, arraylist<long>>> jsonlist = new arraylist<hashmap<string, arraylist<long>>>(); private static hashmap<string, arraylist<long>> mapvalues = new hashmap<string, arraylist<long>>(); //legends private static arraylist<hashmap<string, string>> jsonliststring = new arraylist<hashmap<string, string>>(); private static hashmap<string, string> mapvaluestext = new hashmap<string,string>(); private static arraylist<string> datestringarray = new arraylist<string>(); private graphicalview chartview; private xymultipleseriesrenderer mrenderer; public homefragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_home, container, false); //setretaininstance(true); new jsonparse().execute(); return rootview; } @suppresswarnings("unchecked") @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } @override public void onresume() { super.onresume(); jsonlist.clear(); mapvalues.clear(); jsonliststring.clear(); mapvaluestext.clear(); } private class jsonparse extends asynctask<string, string, jsonobject> { private progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity()); pdialog.setmessage("getting data ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); }//end of onpreexecute @override protected jsonobject doinbackground(string... args) { while(!iscancelled()) { jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); return json; } return null; }//end of doinbackground @override protected void oncancelled(jsonobject json) { toast.maketext(getactivity(), "cancelled. cannot retrieve data @ time. please try again later.", toast.length_short).show(); } @override protected void onpostexecute(jsonobject json) { pdialog.dismiss(); try { //fetching array of elements jsonarray jsonarray = json.getjsonarray("elements"); //fetching textview //fetching pertamina ep textview uid; uid = (textview) getactivity().findviewbyid(r.id.txtlabel); jsonobject jsonelementstext = jsonarray.getjsonobject(jsonarray.length()-1); string text = jsonelementstext.getstring("text"); //fetching date jsonobject jsonobjectxaxis = json.getjsonobject("x_axis"); jsonobject jsonobjectxaxislabels = jsonobjectxaxis.getjsonobject("labels"); jsonarray jsonarrayxaxislabels = jsonobjectxaxislabels.getjsonarray("labels"); for(int i=0;i<jsonarrayxaxislabels.length();i++) { datestringarray.add((string) jsonarrayxaxislabels.getstring(i)); } string jsonarraystringxaxislabels = (string) jsonarrayxaxislabels.getstring(jsonarrayxaxislabels.length()-1); //json string date format char[] datechar = jsonarraystringxaxislabels.tochararray(); string day = ""+datechar[0]+""+datechar[1]+""; string monthno = ""+datechar[3]+""+datechar[4]+""; if(monthno.matches("01")) { monthno="january"; } if(monthno.matches("01")) { monthno="january"; } if(monthno.matches("02")) { monthno="february"; } if(monthno.matches("03")) { monthno="march"; } if(monthno.matches("04")) { monthno="april"; } if(monthno.matches("05")) { monthno="may"; } if(monthno.matches("06")) { monthno="june"; } if(monthno.matches("07")) { monthno="july"; } if(monthno.matches("08")) { monthno="august"; } if(monthno.matches("09")) { monthno="september"; } if(monthno.matches("10")) { monthno="october"; } if(monthno.matches("11")) { monthno="november"; } if(monthno.matches("12")) { monthno="december"; } time today = new time(time.getcurrenttimezone()); today.settonow(); int year = today.year; //fetching number jsonarray jsonarrayvalues = jsonelementstext.getjsonarray("values"); long arrayvaluesmax = jsonarrayvalues.getlong(jsonarrayvalues.length()-1); //fetching text view //uid.settext("total produksi minyak "+text+", "+day+" "+monthno+" "+year+" = "+arrayvaluesmax+" bbls"); uid.settext(html.fromhtml("total produksi minyak <b>"+text+"</b>, "+day+" "+monthno+" "+year+" = <b>"+numberformat.getnumberinstance(locale.german).format(arrayvaluesmax)+"</b> bbls")); jsonobject jsonyaxis = json.getjsonobject("y_axis"); long yaxismin = jsonyaxis.getlong("min"); long yaxismax = jsonyaxis.getlong("max"); (int i=0;i<jsonarray.length();i++) { arraylist<long> list = new arraylist<long>(); hashmap<string, arraylist<long>> mapvalues = new hashmap<string, arraylist<long>>(); jsonobject elementno = (jsonobject) jsonarray.get(i); jsonarray valuesgraph = (jsonarray) elementno.get("values"); string texts = elementno.getstring("text"); int len = valuesgraph.length(); (int x=0;x<len;x++) { if(!valuesgraph.isnull(x)) { long nilai = (long)valuesgraph.getlong(x); list.add(nilai); } } mapvalues.put("values", list); mapvaluestext.put("text", texts); jsonlist.add(mapvalues); jsonliststring.add(mapvaluestext); } //line graph xymultipleseriesdataset dataset = new xymultipleseriesdataset(); mrenderer = new xymultipleseriesrenderer(); mrenderer.setapplybackgroundcolor(true); mrenderer.setbackgroundcolor(color.rgb(254, 254, 254)); mrenderer.setmarginscolor(color.rgb(254, 254, 254)); int[] x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; for(int start=0; start<jsonlist.size(); start++) { start++; mapvaluestext = jsonliststring.get(start-1); timeseries series = new timeseries(mapvaluestext.get("text").trim()); start--; mapvalues = jsonlist.get(start); int jumlahvalues = mapvalues.get("values").size(); for(int i=0; i<jumlahvalues; i++) { series.add(x[i], mapvalues.get("values").get(i)); } dataset.addseries(series); xyseriesrenderer renderer = new xyseriesrenderer(); renderer.setshowlegenditem(true); renderer.setchartvaluestextsize(24); renderer.setcolor(color.rgb(0,108,183)); renderer.setlinewidth(3); renderer.setpointstyle(pointstyle.circle); renderer.setfillpoints(true); //renderer.setdisplaychartvalues(true); mrenderer.addseriesrenderer(renderer); } //xy titles mrenderer.setytitle("production"); //mrenderer.setxtitle("date"); mrenderer.setaxistitletextsize(22); mrenderer.setlabelstextsize(22); mrenderer.setxlabelscolor(color.black); mrenderer.setylabelscolor(0, color.black); mrenderer.setshowlabels(true); //legends mrenderer.setshowlegend(true); //mrenderer.setfitlegend(true); mrenderer.setlegendtextsize(20); mrenderer.setxlabelspadding(20); mrenderer.setylabelsalign(align.right, 0); mrenderer.setylabelspadding(7); mrenderer.setylabelsverticalpadding(-5); if (getresources().getconfiguration().orientation == configuration.orientation_portrait) { (int = 0; < jsonarrayxaxislabels.length();) { string xchartdates = jsonarrayxaxislabels.getstring(i); mrenderer.addtextlabel(i+1, xchartdates); if(mapvalues.get("values").size()<9) { i=i+1; } else { i=i+4; } } mrenderer.setxlabelsalign(align.center); mrenderer.setxlabels(0); } else { (int = 0; < jsonarrayxaxislabels.length(); i++) { string xchartdates = jsonarrayxaxislabels.getstring(i); mrenderer.addtextlabel(i+1, xchartdates); } mrenderer.setxlabelsalign(align.center); mrenderer.setxlabels(0); } mrenderer.setaxescolor(color.black); mrenderer.setlabelscolor(color.black); mrenderer.setmargins(new int[]{ 10, 110, 10, 10}); mrenderer.setyaxismax(yaxismax); mrenderer.setyaxismin(yaxismin); mrenderer.setxaxismin(0); mrenderer.setxaxismax(jsonarrayxaxislabels.length()); mrenderer.setpointsize(9.5f); //mrenderer.setpanlimits(new double[]{0, (jsonarrayxaxislabels.length())*1.1, yaxismin, yaxismax}); mrenderer.setinscroll(true); mrenderer.setclickenabled(true); mrenderer.setpanenabled(true); mrenderer.setzoomenabled(true); mrenderer.setzoomlimits(new double[]{0, (jsonarrayxaxislabels.length())*1.1, yaxismin, yaxismax}); //grid mrenderer.setshowgrid(true); mrenderer.setshowcustomtextgrid(true); mrenderer.setgridcolor(color.rgb(162,172,186)); if(chartview!=null) { chartview.repaint(); } else{ chartview = chartfactory.getlinechartview(getactivity(), dataset, mrenderer); } chartview.addzoomlistener(new zoomlistener() { @override public void zoomreset() { // todo auto-generated method stub } @override public void zoomapplied(zoomevent arg0) { // todo auto-generated method stub } }, true, true); chartview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { seriesselection seriesselection = chartview.getcurrentseriesandpoint(); if (seriesselection == null) { chartview.repaint(); } else { long values = (long) seriesselection.getvalue(); mapvaluestext = jsonliststring.get(seriesselection.getseriesindex()); int xvalue = (int)seriesselection.getxvalue()-1; toast.maketext(getactivity(), "total produksi minyak "+mapvaluestext.get("text")+", "+datestringarray.get(xvalue)+" = " +numberformat.getnumberinstance(locale.german).format(values) +" bbls", toast.length_long).show(); } } }); linearlayout layout = (linearlayout) getactivity().findviewbyid(r.id.dashboard_chart_layout); layout.removeallviews(); layout.addview(chartview); textview failedtext; failedtext = (textview) getactivity().findviewbyid(r.id.txtfailedlabel_home); failedtext.setvisibility(view.gone); } catch (jsonexception e) { e.printstacktrace(); toast.maketext(getactivity(), "jsonexception. cannot retrieve data @ time. please try again later.", toast.length_short).show(); textview failedtext; failedtext = (textview) getactivity().findviewbyid(r.id.txtfailedlabel_home); failedtext.settext("please retry"); } catch(exception e){ e.printstacktrace(); toast.maketext(getactivity(), "exception. cannot retrieve data @ time. please try again later.", toast.length_short).show(); textview failedtext; failedtext = (textview) getactivity().findviewbyid(r.id.txtfailedlabel_home); failedtext.settext("please retry"); } }//end of doinbackground }}
Comments
Post a Comment