Android Exception "JSONArray fail" while fetching data from sql server using c# JSON webservice -


i developing android app , c# desktop app. c# application connecting sql server database. reading/sending data in android database using json c# web service.

i using code retrieve data database , display in android in table. here android code:

public class studentactivity extends activity {        public void oncreate(bundle savedinstancestate)       {             super.oncreate(savedinstancestate);             setcontentview(r.layout.student);              button button2 = (button) findviewbyid(r.id.button2);               button2.setonclicklistener(new view.onclicklistener()             {             public void onclick(view view)               {                  string rs = null;                 inputstream = null;                  try{                         httpclient httpclient = new defaulthttpclient();                         httppost httppost = new httppost("http://10.0.2.2:51220/service1.svc/getdata?");                         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                 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();                          rs=sb.tostring();                 }                 catch(exception e)                 {                        log.e("log_tag", "error converting rs "+e.tostring());                     toast.maketext(getapplicationcontext(), " input reading fail", toast.length_short).show();                  }                  //parse json data                 try                 {                  jsonarray jarray = new jsonarray(rs);                   string re=jarray.getstring(jarray.length()-1);                   tablelayout tv=(tablelayout) findviewbyid(r.id.table);                 tv.removeallviewsinlayout();                      int flag=1;                  for(int i=-1;i<jarray.length()-1;i++)                          {                         tablerow tr=new tablerow(studentactivity.this);                                  tr.setlayoutparams(new layoutparams(                                            layoutparams.fill_parent,                                            layoutparams.wrap_content));                                     if(flag==1)                                 {                                      textview b6=new textview(studentactivity.this);                                      b6.settext("id");                                      b6.settextcolor(color.blue);                                      b6.settextsize(15);                                      tr.addview(b6);                                       textview b19=new textview(studentactivity.this);                                      b19.setpadding(10, 0, 0, 0);                                      b19.settextsize(15);                                      b19.settext("first name");                                      b19.settextcolor(color.blue);                                      tr.addview(b19);                                     textview b29=new textview(studentactivity.this);                                  b29.setpadding(10, 0, 0, 0);                                      b29.settext("last name");                                      b29.settextcolor(color.blue);                                      b29.settextsize(15);                                      tr.addview(b29);                                        textview b4=new textview(studentactivity.this);                                  b4.setpadding(10, 0, 0, 0);                                      b4.settext("project");                                      b4.settextcolor(color.blue);                                      b4.settextsize(15);                                      tr.addview(b4);                                        textview b5=new textview(studentactivity.this);                                  b5.setpadding(10, 0, 0, 0);                                      b5.settext("date , time");                                      b5.settextcolor(color.blue);                                      b5.settextsize(15);                                      tr.addview(b5);                                       tv.addview(tr);                                       final view vline = new view(studentactivity.this);                                           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")+                                               ", firstname: "+json_data.getstring("f2")+                                               ", lastname: "+json_data.getint("f3") +                                               ", project: "+json_data.getstring("f4")+                                               ", datetime: "+json_data.getstring("f5"));                                 textview b=new textview(studentactivity.this);                                 string stime=string.valueof(json_data.getint("f1"));                                   b.settext(stime);                                 b.settextcolor(color.red);                                 b.settextsize(15);                                 tr.addview(b);                                  textview b1=new textview(studentactivity.this);                                 b1.setpadding(10, 0, 0, 0);                                 b1.settextsize(15);                                 string stime1=json_data.getstring("f2");                                  b1.settext(stime1);                                 b1.settextcolor(color.white);                                 tr.addview(b1);                                textview b2=new textview(studentactivity.this);                              b2.setpadding(10, 0, 0, 0);                                 string stime2=string.valueof(json_data.getint("f3"));                                 b2.settext(stime2);                                 b2.settextcolor(color.red);                                 b2.settextsize(15);                                 tr.addview(b2);                                   textview b3=new textview(studentactivity.this);                              b3.setpadding(10, 0, 0, 0);                                 string stime3=string.valueof(json_data.getint("f4"));                                 b3.settext(stime3);                                 b3.settextcolor(color.white);                                 b3.settextsize(15);                                 tr.addview(b3);                                   textview b4=new textview(studentactivity.this);                              b4.setpadding(10, 0, 0, 0);                                 string stime4=string.valueof(json_data.getint("f5"));                                 b4.settext(stime4);                                 b4.settextcolor(color.red);                                 b4.settextsize(15);                                 tr.addview(b4);                                    tv.addview(tr);                           final view vline1 = new view(studentactivity.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();                 }             }            });          }   } 

while running when clicked button retrieve data database app catching exception

catch(jsonexception e)                     {                             log.e("log_tag", "error parsing data "+e.tostring());                             toast.maketext(getapplicationcontext(), "jsonarray fail", toast.length_short).show();                     } 

i not getting why catching exception , displaying "jsonarray fail" because sample code , have started learn json webservice. please me solve it. if want more information please tell me provide you. thanks


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -