Parsing simple JSON to android -


having problem parsing json data android

  • i have descriptively mentioned problem facing, ideas on how overcome this

initially have used json url

url:: http://54.218.73.244:8084/

jsonparser.java

public class jsonparser {      static inputstream = null;     static jsonarray jobj = null;     static string json = "";      // constructor     public jsonparser() {      }      public jsonarray getjsonfromurl(string url) {          // making http request         try {             // defaulthttpclient             defaulthttpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(url);              httpresponse httpresponse = httpclient.execute(httppost);             httpentity httpentity = httpresponse.getentity();             = httpentity.getcontent();                     } catch (unsupportedencodingexception e) {             e.printstacktrace();         } catch (clientprotocolexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         }          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();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());         }          // try parse string json object         try {             jobj = new jsonarray(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.tostring());         }          // return json string         return jobj;      } } 

androidjsonparsingactivity.java

    public class androidjsonparsingactivity extends activity {          // url make request         private static string url = "http://54.218.73.244:8084/";         private hashmap<integer, string> contentsmap = new hashmap<integer, string>();           @override         protected void oncreate(bundle savedinstancestate) {             super.on  create(savedinstancestate);         setcontentview(r.layout.activity_main);             // creating json parser instance             jsonparser jparser = new jsonparser();              // getting json string url             jsonarray json = jparser.getjsonfromurl(url);              try {                 (int = 0; < json.length(); i++) {                     jsonobject c = json.getjsonobject(i);                      // storing each json item in variable                     int id = c.getint("id");                     string name = c.getstring("content");                      contentsmap.put(id, name);                 }             } catch (jsonexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }      @override     protected void onresume() {         super.onresume();          textview textview=(textview) findviewbyid(r.id.textview1);         textview.settext(contentsmap.get(1));          textview=(textview) findviewbyid(r.id.textview2);         textview.settext(contentsmap.get(2));       } } 

activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"      android:padding="10sp">      <textview         android:id="@+id/textview1"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:padding="10sp"         android:text="textview1" />      <textview         android:id="@+id/textview2"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:padding="10sp"         android:text="textview2" />  </linearlayout> 

output::

the output above code obtained is

fig-1


next change url

url:: http://54.218.73.244:7003/

and without changing other part of code

now error screen as

fig-2


error log

08-10 09:45:41.731: e/json parser(448): error parsing data org.json.jsonexception: value cannot of type java.lang.string cannot converted jsonarray 08-10 09:45:41.737: d/androidruntime(448): shutting down vm 08-10 09:45:41.737: w/dalvikvm(448): threadid=1: thread exiting uncaught exception (group=0x40015560) 08-10 09:45:41.772: e/androidruntime(448): fatal exception: main 08-10 09:45:41.772: e/androidruntime(448): java.lang.runtimeexception: unable start activity componentinfo{com.example.json_web/com.example.json_web.androidjsonparsingactivity}: java.lang.nullpointerexception 08-10 09:45:41.772: e/androidruntime(448):  @ android.app.activitythread.performlaunchactivity(activitythread.java:1647) 08-10 09:45:41.772: e/androidruntime(448):  @ android.app.activitythread.handlelaunchactivity(activitythread.java:1663) 08-10 09:45:41.772: e/androidruntime(448):  @ android.app.activitythread.access$1500(activitythread.java:117) 08-10 09:45:41.772: e/androidruntime(448):  @ android.app.activitythread$h.handlemessage(activitythread.java:931) 

my analysis according error ::

  • there error parsing string cannot converted jsonarray
  • how solve error ?

  • for url :: http://54.218.73.244:8084/

json reply :: [{"id":1,"content":"hello"},{"id":2,"content":"world"}]

  • for url :: http://54.218.73.244:7003/

json reply :: [{"id":1,"content":"hello"},{"id":2,"content":"world"}]

structure of json same, please confirm using url in browser


thanks,


your server @ http://54.218.73.244:7003/ not support post request.

so either make server accept post requests or change following line in jsonparser class.

//httppost httppost = new httppost(url); //remove , replace below  httpget httpget = new httpget(url); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -