android - AsyncTask in Fragment not working and shows FATAL EXCEPTION: AsyncTask #1 -
the following code when run crashes app. calling in mainactivity
. when run tells me:
fatal exception: asynctask #1
, directs me toward progressdialog
, http response
line
import android.app.fragment; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import com.montel.senarivesselapp.model.showdatalist; import com.montel.senarivesselapp.model.vessel; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.statusline; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpresponseexception; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.util.arraylist; public class showallfragment extends fragment { //variables json query private static final string name ="vessel_name"; private static final string etad="eta_date"; private static final string etat="eta_time"; private static final string etbd="etb_date"; private static final string etbt="etb_time"; private static final string shippingname="shipping_agent_name"; private static final string v1 = "vessels"; private static final string v2 = "vessel"; private arraylist<vessel> vlist = new arraylist<>(); private arrayadapter arrayadapter = null; private listview listview = null; private edittext et = null; private showdatalist ssadapter = null; private view rootview; public showallfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { rootview = inflater.inflate(r.layout.fragment_showall, container, false); setcontentview(r.layout.fragment_showall); setretaininstance(true); return rootview; } private void setcontentview(int fragment_showall) { } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } //for getting json schedule server class schedule extends asynctask<string, string, string> { progressdialog loaddialog; @override protected void onpreexecute() { super.onpreexecute(); //show loading dialog loaddialog = new progressdialog(showallfragment.this); loaddialog.setmessage("please wait...."); loaddialog.setcancelable(false); loaddialog.show(); } @override protected string doinbackground(string... uri) { bufferedreader input = null; string data = null; try { defaulthttpclient client = new defaulthttpclient(); httpresponse response = client.execute(new httpget("http://")); statusline stline = response.getstatusline(); if (stline.getstatuscode() == httpstatus.sc_ok) { bytearrayoutputstream out = new bytearrayoutputstream(); response.getentity().writeto(out); data = out.tostring(); out.close(); } else { response.getentity().getcontent().close(); throw new ioexception(stline.getreasonphrase()); } } catch (httpresponseexception he) { he.printstacktrace(); } catch (clientprotocolexception cpe) { cpe.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (input != null) { try { input.close(); return data; } catch (exception e) { system.out.println(e); } } } return data; } @override protected void onpostexecute(string result) { super.onpostexecute(result); //fill vlist array log.d("post execute", result.tostring()); loaddialog.dismiss(); if (result != null) { try { jsonobject jo = new jsonobject(result); jsonarray vessels = jo.getjsonarray(v1); log.d("vessel log", vessels.tostring()); vlist = new arraylist(); (int = 0; < vessels.length(); i++) { jsonobject vv = vessels.getjsonobject(i); log.d("vessel name", vv.getjsonobject(v2).getstring(name)); vlist.add(new vessel(vv.getjsonobject(v2).getstring(name), vv.getjsonobject(v2).getstring(etad), vv.getjsonobject(v2).getstring(etat), vv.getjsonobject(v2).getstring(etbd), vv.getjsonobject(v2).getstring(etbt), vv.getjsonobject(v2).getstring(shippingname))); } } catch (jsonexception e) { e.printstacktrace(); } } listview = (listview) rootview.findviewbyid(r.id.datashow); listview.setadapter(arrayadapter); ssadapter = new showdatalist(showallfragment.this , vlist); listview.setadapter(ssadapter); } } }
securityexception: permission denied (missing internet permission
so should request internet permission in projects androidmanifest.xml
file. add on rigth place:
<uses-permission android:name="android.permission.internet" />
Comments
Post a Comment