android - Parsing JSON to Array Map Produces "The local variable may not have been initialized" -


i'm following tutorial ( http://mobile.dzone.com/news/android-tutorial-how-parse ) how ever // add items doddataitemobj dataitem string - string publiceventtype = d.getstring(publiceventtype); saying local variable publiceventtype may not have been initialized. points line jsonobject d = dataitems.getjsonobject(i); believe defined. i've tried no luck public string d; produces error code have mentioned.

i'd appreciate thoughts.

import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.hashmap; 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.util.log;   public class parsejson {  {          // hashmap listview     arraylist<hashmap<string, string>> datalist = new arraylist<hashmap<string, string>>();      //get data (public static jsonobject getjsonfromurl(string url))   //jsonobject json = parsejson.getjsonfromurl("http://api.geonames.org/postalcodesearchjson?formatted=true&postalcode=9791&maxrows=10&username=demo&style=full");   jsonobject json = parsejson.getjsonfromurl(globals.parsejsonurl);              try{     //get element holds dataitem ( jsonarray )     jsonarray dataitems = json.getjsonarray("dataitem");                      //loop array           for(int i=0;i < dataitems.length();i++){                                    // creating new hashmap               hashmap<string, string> map = new hashmap<string, string>();              jsonobject d = dataitems.getjsonobject(i);              // d not working ?              // add items doddataitemobj dataitem string              string publiceventtype = d.getstring(publiceventtype);                             // adding each child node hashmap key => value             map.put(publiceventtype, publiceventtype);              // adding hashlist arraylist             datalist.add(map);     }          }catch(exception e)        {              e.printstacktrace();              log.e("log_tag", "error parsing data "+e.tostring());            }          }  public static jsonobject getjsonfromurl(string url){      //initialize     inputstream = null;     string result = "";     jsonobject jarray = null;      //http post     try{         httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost(url);         httpresponse response = httpclient.execute(httppost);         httpentity entity = response.getentity();         = entity.getcontent();      }catch(exception e){         log.e("log_tag", "error in http connection "+e.tostring());     }      //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");         }         is.close();         result=sb.tostring();     }catch(exception e){         log.e("log_tag", "error converting result "+e.tostring());     }      //try parse string json object     try{             jarray = new jsonobject(result);     }catch(jsonexception e){         log.e("log_tag", "error parsing data "+e.tostring());     }      return jarray; }  } 

 string publiceventtype = d.getstring("publiceventtype");  

or

 string publiceventtype = d.getstring("publiceventtype"); // more conventionnal 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -