android - json exception no value for f1 -
i have made android application , trying select data database table-row , textview.
but when trying handle json response ,getting error, here's code
public void selectfromdb(){ try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://xxxxxxxx/selectall.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(getactivity(), "connection fail", toast.length_short).show(); } //convert response string try { bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb = new stringbuilder(); 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(getactivity(), " 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)getview().findviewbyid(r.id.table); tv.removeallviewsinlayout(); int flag=1; for(int i=-1;i<jarray.length()-1;i++) { tablerow tr=new tablerow(getactivity()); tr.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); if(flag==1) { textview b6=new textview(getactivity()); b6.settext("id"); b6.settextcolor(color.blue); b6.settextsize(15); tr.addview(b6); textview b19=new textview(getactivity()); b19.setpadding(10, 0, 0, 0); b19.settextsize(15); b19.settext("matier"); b19.settextcolor(color.blue); tr.addview(b19); tv.addview(tr); final view vline = new view(getactivity()); vline.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.fill_parent, 2)); vline.setbackgroundcolor(color.blue); tv.addview(vline); flag=0; }else { jsonobject json_data = jarray.getjsonobject(i); log.i("log_tag","id: "+json_data.getint("f1")+ ", matier: "+json_data.getstring("f2") ); textview b=new textview(getactivity()); // string stime=string.valueof(json_data.getint("f1")); b.settext(json_data.optstring("f1")); b.settextcolor(color.red); b.settextsize(15); tr.addview(b); textview b1=new textview(getactivity()); b1.setpadding(10, 0, 0, 0); b1.settextsize(15); string stime1=json_data.optstring("f2"); b1.settext(stime1); b1.settextcolor(color.white); tr.addview(b1); tv.addview(tr); final view vline1 = new view(getactivity()); 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(getactivity(), e.tostring(), toast.length_short).show(); } } }
and code php
<?php $con = mysql_connect("","",""); if (!$con) { die('could not connect: ' . mysql_error()); } mysql_select_db("databas", $con); $i=mysql_query("select * test",$con); $num_rows = mysql_num_rows($i); $check=''; while($row = mysql_fetch_array($i)) { $r[]=$row; $check=$row['f1']; } if($check==null) { $r[$num_rows]="record not available"; echo json_encode($r); } else { $r[$num_rows]="success"; echo json_encode($r); } mysql_close($con); ?>
json exception no value 'f1'
ths out put of php file
[{"0":"66","id":"66","1":"","matier":""},{"0":"65","id":"65","1":"","matier":""},{"0":"64","id":"64","1":"","matier":""},{"0":"63","id":"63","1":"","matier":""},{"0":"62","id":"62","1":"","matier":""},{"0":"61","id":"61","1":"","matier":""},{"0":"60","id":"60","1":"","matier":""},{"0":"59","id":"59","1":"","matier":""},{"0":"58","id":"58","1":"","matier":""},{"0":"57","id":"57","1":"","matier":""},{"0":"56","id":"56","1":"","matier":""},{"0":"55","id":"55","1":"","matier":""},{"0":"54","id":"54","1":"test","matier":"test"},{"0":"53","id":"53","1":"jfhhf","matier":"jfhhf"},{"0":"52","id":"52","1":"jfhhf","matier":"jfhhf"},{"0":"42","id":"42","1":"ykk","matier":"ykk"},{"0":"51","id":"51","1":"jfhhf","matier":"jfhhf"},{"0":"50","id":"50","1":"jfhhf","matier":"jfhhf"},{"0":"49","id":"49","1":"opp","matier":"opp"},{"0":"48","id":"48","1":"opp","matier":"opp"},{"0":"47","id":"47","1":"opp","matier":"opp"},{"0":"46","id":"46","1":"opp","matier":"opp"},{"0":"45","id":"45","1":"opp","matier":"opp"},{"0":"44","id":"44","1":"ykfvjf","matier":"ykfvjf"},{"0":"43","id":"43","1":"ykfvjf","matier":"ykfvjf"},"record not available"]
Comments
Post a Comment