android - Issues with JSON Parsing Objects and Arrays -


im having issue following code im using parse json objects , arrays. please see following json data im trying gettext , insert textviews. reason im getting error summary cannot found. please see jsonparser class details.

trying parse following url summary data.

https://api.company.com/api/systems/165756/summary?&key=e1e63de7276b04c9bb99adfd45b3a14c

checking url in json validater (http://jsonlint.com/) following

 {   "energy_month": 31132,   "current_power": 4210,   "modules": 24,   "energy_today": 14666,   "system_id": 165756,   "energy_week": 215504,   "source": "microinverters",   "energy_lifetime": 1545467,   "summary_date": "2013-05-02t00:00:00-07:00"  } 

how here code im using try , parse few of these strings (energy_month) (current_power) etc. in jsonparser.java have class parse either array or object. may need fixed.

 protected arraylist<string> doinbackground(final string... args) {              jsonparser jparser = new jsonparser();              arrfortextviews=new arraylist<string>();             // using apikey strings.xml             string apikey = getstring(r.string.apikey);              sharedpreferences settings = getsharedpreferences(prefs_name, 0);             string systemid = settings.getstring("systemid", "");              //jsonarray json = jparser.getjsonfromurl(url2 + systemid + "/summary?&key=" + apikey);             jsonobject json2 = jparser.getjsonfromurl2(url2 + systemid + "/summary?&key=" + apikey);             //for (int = 0; < json2.length(); i++) {                 //i = index                 try {                     //jsonobject c = json2.getjsonobject(i);                     //jsonobject geometry = json2.getjsonobject("summary");                     log.e("json parser", json2 + args.tostring());                      string current_power = json2.getstring(tag_current_power);                     string energy_lifetime = json2.getstring(tag_energy_lifetime);                      arrfortextviews.add(current_power);                     arrfortextviews.add(energy_lifetime);                  } catch (jsonexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                     log.e("json parser", json2 + e.tostring());                 }             return arrfortextviews;          } 

jsonparser.class

   public class jsonparser {  private static final context context = null; static inputstream = null; static jsonobject jarray = null; static jsonarray jarray2 = null; static string json = "";  // constructor public jsonparser() {  } public jsonobject getjsonfromurl2(string url) {         stringbuilder builder = new stringbuilder();         httpclient client = new defaulthttpclient();         httpget httpget = new httpget(url);         try {           httpresponse response = client.execute(httpget);           statusline statusline = response.getstatusline();           int statuscode = statusline.getstatuscode();           if (statuscode == 200) {             httpentity entity = response.getentity();             inputstream content = entity.getcontent();             bufferedreader reader = new bufferedreader(new inputstreamreader(content));             string line;             while ((line = reader.readline()) != null) {               builder.append(line);             }           } else {             log.e("==>", "no response, check api key");             toast.maketext(context,"error response, check api key", toast.length_long).show();           }         } catch (clientprotocolexception e) {           e.printstacktrace();         } catch (ioexception e) {           e.printstacktrace();         }     // try parse string json object     try {         jsonobject jobj = new jsonobject(builder.tostring());         jarray = jobj.getjsonobject("summary");         log.e("json parser", jobj + url.tostring());     } catch (jsonexception e) {         log.e("json parser", "error parsing data " + e.tostring());         log.e("json parser", json + url + e.tostring());     }      // return json string     return jarray;  } public jsonarray getjsonfromurl(string url) {         stringbuilder builder = new stringbuilder();         httpclient client = new defaulthttpclient();         httpget httpget = new httpget(url);         try {           httpresponse response = client.execute(httpget);           statusline statusline = response.getstatusline();           int statuscode = statusline.getstatuscode();           if (statuscode == 200) {             httpentity entity = response.getentity();             inputstream content = entity.getcontent();             bufferedreader reader = new bufferedreader(new inputstreamreader(content));             string line;             while ((line = reader.readline()) != null) {               builder.append(line);             }           } else {             log.e("==>", "no response, check api key");             toast.maketext(context,"error response, check api key", toast.length_long).show();           }         } catch (clientprotocolexception e) {           e.printstacktrace();         } catch (ioexception e) {           e.printstacktrace();         }      // try parse string json object     try {         jsonobject jobj = new jsonobject(builder.tostring());         jarray2 = jobj.getjsonarray("systems");         log.e("json parser", jarray2 + json + url.tostring());     } catch (jsonexception e) {         log.e("json parser", "error parsing data " + e.tostring());         log.e("json parser", json + url + e.tostring());     }     // return json string     return jarray2;    } 


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -