java - How to display table from the database using json response in Android? (I'm getting JsonArray Fail error in this code) -
this attendance.java file
package com.glbwebsim; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.tablelayout; import android.widget.tablerow;` import android.widget.textview; import android.widget.toast; import android.widget.tablerow.layoutparams; public class attendance extends activity { static inputstream istream = null; static jsonarray jarray = null; static string json = ""; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_main7); button details = (button) findviewbyid(r.id.mydetails); details.setonclicklistener(new view.onclicklistener() { @suppresswarnings({ "deprecation", "unused" }) public void onclick(view view) { string result = null; inputstream = null; try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://frndz4ever.com/glbsim/attend.php"); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); log.e("log_tag", "connection success "); // toast.maketext(getapplicationcontext(), "pass", toast.length_short).show(); } catch(exception e) { log.e("log_tag", "error in http connection "+e.tostring()); toast.maketext(getapplicationcontext(), "connection fail", toast.length_short).show(); } //convert response string stringbuilder sb = new stringbuilder(); try { bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); // toast.maketext(getapplicationcontext(), "input reading pass", toast.length_short).show(); } is.close(); result=sb.tostring(); } catch(exception e) { log.e("log_tag", "error converting result "+e.tostring()); toast.maketext(getapplicationcontext(), " input reading fail", toast.length_short).show(); } //parse json data try { jsonarray jarray = new jsonarray("["+result+"]"); string re=jarray.getstring(jarray.length()-1); tablelayout tv=(tablelayout) findviewbyid(r.id.table); tv.removeallviewsinlayout(); int flag=1; for(int i=0;i<re.length();i++) { tablerow tr=new tablerow(attendance.this); tr.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); if(flag==1) { textview b6=new textview(attendance.this); b6.settext("roll_no"); b6.settextcolor(color.black); b6.settextsize(18); tr.addview(b6); textview b19=new textview(attendance.this); b19.setpadding(10, 0, 0, 0); b19.settextsize(18); b19.settext("subjects"); b19.settextcolor(color.black); tr.addview(b19); textview b29=new textview(attendance.this); b29.setpadding(10, 0, 0, 0); b29.settext("present"); b29.settextcolor(color.black); b29.settextsize(18); tr.addview(b29); textview b30=new textview(attendance.this); b30.setpadding(10, 0, 0, 0); b30.settext("absent"); b30.settextcolor(color.black); b30.settextsize(18); tr.addview(b30); tv.addview(tr); final view vline = new view(attendance.this); vline.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.fill_parent, 2)); vline.setbackgroundcolor(color.blue); tv.addview(vline); } else { jsonobject json_data = jarray.getjsonobject(i); log.i("log_tag","roll_no: "+json_data.getint("roll_no")+ ", subjects: "+json_data.getstring("subjects")+ ", present: "+json_data.getint("present")+ ",absent: "+json_data.getint("absent")); textview b=new textview(attendance.this); string stime=string.valueof(json_data.getint("roll_no")); b.settext(stime); b.settextcolor(color.red); b.settextsize(15); tr.addview(b); textview b1=new textview(attendance.this); b1.setpadding(10, 0, 0, 0); b1.settextsize(15); string stime1=json_data.getstring("subjects"); b1.settext(stime1); b1.settextcolor(color.white); tr.addview(b1); textview b2=new textview(attendance.this); b2.setpadding(10, 0, 0, 0); string stime2=string.valueof(json_data.getint("present")); b2.settext(stime2); b2.settextcolor(color.red); b2.settextsize(15); tr.addview(b2); textview b3=new textview(attendance.this); b3.setpadding(10, 0, 0, 0); string stime3=string.valueof(json_data.getint("absent")); b3.settext(stime2); b3.settextcolor(color.red); b3.settextsize(15); tr.addview(b3); tv.addview(tr); final view vline1 = new view(attendance.this); vline1.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.fill_parent, 1)); vline1.setbackgroundcolor(color.white); tv.addview(vline1); } }} catch(jsonexception e) { log.e("log_tag", "error parsing data "+e.tostring()); toast.maketext(getapplicationcontext(), "jsonarray fail", toast.length_short).show(); } return; } }); }}
i'm getting jsonarray fail error on click of button. please help.
logcat:
04-20 20:54:37.458: d/strictmode(9281): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1151) 04-20 20:54:37.458: d/strictmode(9281): @ libcore.io.blockguardos.recvfrom(blockguardos.java:163) 04-20 20:54:37.458: d/strictmode(9281): @ libcore.io.iobridge.recvfrom(iobridge.java:506)
see code below reference, might have errors, ides how works.
any network activity should performed in doinbackground
. not perform action on ui.
once doinbackground
complete, data returned onpostexecute
. can perform ui actions here.
public class attendance extends activity { static inputstream istream = null; static jsonarray jarray = null; static string json = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button details = (button) findviewbyid(r.id.mydetails); details.setonclicklistener(new view.onclicklistener() { @suppresswarnings({ "deprecation", "unused" }) public void onclick(view view) { new makehttprequest().execute(); } }); } private class makehttprequest extends asynctask<string, string, string> { @override protected string doinbackground(string... params) { // todo auto-generated method stub string result = null; inputstream = null; try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://frndz4ever.com/glbsim/attend.php"); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); log.i("log_tag", "connection success "); } catch (exception e) { log.e("log_tag", "error in http connection " + e.tostring()); // toast.maketext(getapplicationcontext(), "connection fail", // toast.length_short).show(); } // convert response string stringbuilder sb = new stringbuilder(); try { bufferedreader reader = new bufferedreader( new inputstreamreader(is, "iso-8859-1"), 8); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } return result; } protected void onpostexecute(string result) { // parse json data try { jsonarray jarray = new jsonarray(result); string re = jarray.getstring(jarray.length() - 1); int flag = 1; for(int i=0;i<jarray.length();i++){ jsonobject json_data = jarray.getjsonobject(0); log.i("log_tag", "index: "+ + " roll_no: " + json_data.getint("roll_no") + ", subjects: " + json_data.getstring("subject") + ", present: " + json_data.getint("present") + ",absent: " + json_data.getint("absent")); } // perform required actions here } catch (jsonexception e) { log.e("log_tag", "error parsing data " + e.tostring()); toast.maketext(getapplicationcontext(), "jsonarray fail", toast.length_short).show(); } } }
}
i have updated private class makehttprequest
dont forget add
<uses-permission android:name="android.permission.internet" />
under uses-sdk
tag
and logcat
output:
04-21 23:37:13.904: i/log_tag(14032): index: 0 roll_no: 70, subjects: multimedia, present: 20,absent: 10 04-21 23:37:13.905: i/log_tag(14032): index: 1 roll_no: 70, subjects: multimedia, present: 20,absent: 10 04-21 23:37:13.905: i/log_tag(14032): index: 2 roll_no: 70, subjects: multimedia, present: 20,absent: 10 04-21 23:37:13.905: i/log_tag(14032): index: 3 roll_no: 70, subjects: multimedia, present: 20,absent: 10
first try this, if works, add code step step enhance app.
Comments
Post a Comment