Android JSON Parsing from URL -


i'm trying json project in android. code.

for reason data isn't displayed in textbox runs. i'm not sure made wrong.

this url trying grab data: http://cocoalabs.in/demo/ego/index.php/home/read_all_users

am trying grab 4 strings , display them textviews

this code:

mainactivity.java

public class mainactivity extends activity {  static string cocoainfo="http://cocoalabs.in/demo/ego/index.php/home/read_all_users"; static string clfname = ""; static string cllname = ""; static string cllocation = ""; static string clph1 = "";   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      //to perform background tasks, grabbing info website , write gui     new myasynctask().execute(); }  public class myasynctask extends asynctask<string, string, string> {      @override     protected string doinbackground(string... strings)     {         /*get http client scores streaming uploads & downloads         here download restful information cocoalabs url*/         defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams());          /*use post method grab data url*/         httppost httppost = new httppost(cocoainfo);          /*define type of service we're using - json service*/         httppost.setheader("content-type", "application/json");          /*to read data url*/         inputstream inputstream = null;          /*to hold data url*/         string result = null;          try         {             /*asking response web service*/             httpresponse response = httpclient.execute(httppost);              /*has content url along header , other info*/             httpentity entity = response.getentity();              /*get main content url*/             inputstream = entity.getcontent();              /*read data stream until buffer full. helps grab in bunch*/             bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"),8);              /*creating stringbuilder store read data*/             stringbuilder thestringbuilder = new stringbuilder();              /*for storing read line*/             string line = null;              /*read data buffer until nothing left*/             while((line = reader.readline())!=null)             {                 /*append each line builder object*/                 thestringbuilder.append(line + "\n");             }              /*after finishing reading data*/             result = thestringbuilder.tostring();          }          catch (clientprotocolexception e)         {             e.printstacktrace();         }          catch (ioexception e)         {             e.printstacktrace();         }                  {             try             {                 /*close inputstream*/                 if (inputstream!=null)                     inputstream.close();             }             catch (ioexception e)             {                 e.printstacktrace();             }         }          /*create json object hold bunch of key-value pairs json source*/         jsonobject jsonobject;          /*to print information downloaded in logcat debugging*/         log.v("jsonparser result ", result);           try         {             /*get json object data*/             jsonobject = new jsonobject(result);              clfname = jsonobject.getstring("fname");             cllname = jsonobject.getstring("lname");             cllocation = jsonobject.getstring("location");             clph1 = jsonobject.getstring("ph1");              log.v("jsonparser result", clfname);             log.v("jsonparser result", cllname);             log.v("jsonparser result", cllocation);             log.v("jsonparser result", clph1);          }         catch (jsonexception e)         {             e.printstacktrace();         }          return result;      }      //this method called after background tasks done     /*writes information textviews layout*/     @override     protected void onpostexecute(string result)     {         /*find buttons on screen*/         textview fnametv = (textview) findviewbyid(r.id.fnametv);         textview lnametv = (textview) findviewbyid(r.id.lnametv);         textview locationtv = (textview) findviewbyid(r.id.locationtv);         textview ph1tv = (textview) findviewbyid(r.id.ph1tv);          /*change value displayed on textviews*/         fnametv.settext("first name is" +clfname);         lnametv.settext("last name is" +cllname);         locationtv.settext("location is" +cllocation);         ph1tv.settext("phone number is" +clph1);      } } 

}

try below:

try {   jsonarray main = new jsonarray(responsestring);    for( i=0;i<main.length();i++){     jsonobject mainobj = main.getjsonobject(i);     string uid = mainobj.getstring("uid");     string uname = mainobj.getstring("uname");    }   } catch (jsonexception e) {        e.printstacktrace();  } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -