Android JSON Parsing not working -


i'm using androidhive tutorials data json file , view list or grid. when run application, app crashes , exits. couldn't find error on code. can download source code above tutorial link. provided class files here. please me solve problem possible. in advanced.

androidjsonparsingactivity.java

package com.example.samplejsonparsing;  import java.util.arraylist; import java.util.hashmap;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview;  public class androidjsonparsingactivity extends listactivity {      // url make request     private static string url = "http://api.androidhive.info/contacts/";      // json node names     private static final string tag_contacts = "contacts";     private static final string tag_id = "id";     private static final string tag_name = "name";     private static final string tag_email = "email";     private static final string tag_address = "address";     private static final string tag_gender = "gender";     private static final string tag_phone = "phone";     private static final string tag_phone_mobile = "mobile";     private static final string tag_phone_home = "home";     private static final string tag_phone_office = "office";      // contacts jsonarray     jsonarray contacts = null;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          // hashmap listview         arraylist<hashmap<string, string>> contactlist = new arraylist<hashmap<string, string>>();          // creating json parser instance         jsonparser jparser = new jsonparser();          // getting json string url         jsonobject json = jparser.getjsonfromurl(url);          try {             // getting array of contacts             contacts = json.getjsonarray(tag_contacts);              // looping through contacts             for(int = 0; < contacts.length(); i++){                 jsonobject c = contacts.getjsonobject(i);                  // storing each json item in variable                 string id = c.getstring(tag_id);                 string name = c.getstring(tag_name);                 string email = c.getstring(tag_email);                 string address = c.getstring(tag_address);                 string gender = c.getstring(tag_gender);                  // phone number agin json object                 jsonobject phone = c.getjsonobject(tag_phone);                 string mobile = phone.getstring(tag_phone_mobile);                 string home = phone.getstring(tag_phone_home);                 string office = phone.getstring(tag_phone_office);                  // creating new hashmap                 hashmap<string, string> map = new hashmap<string, string>();                  // adding each child node hashmap key => value                 map.put(tag_id, id);                 map.put(tag_name, name);                 map.put(tag_email, email);                 map.put(tag_phone_mobile, mobile);                  // adding hashlist arraylist                 contactlist.add(map);             }         } catch (jsonexception e) {             e.printstacktrace();         }           /**          * updating parsed json data listview          * */         listadapter adapter = new simpleadapter(this, contactlist,                 r.layout.list_item,                 new string[] { tag_name, tag_email, tag_phone_mobile }, new int[] {                         r.id.name, r.id.email, r.id.mobile });          setlistadapter(adapter);          // selecting single listview item         listview lv = getlistview();          // launching new screen on selecting single listitem         lv.setonitemclicklistener(new onitemclicklistener() {              @override             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                 // getting values selected listitem                 string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring();                 string cost = ((textview) view.findviewbyid(r.id.email)).gettext().tostring();                 string description = ((textview) view.findviewbyid(r.id.mobile)).gettext().tostring();                  // starting new intent                 intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class);                 in.putextra(tag_name, name);                 in.putextra(tag_email, cost);                 in.putextra(tag_phone_mobile, description);                 startactivity(in);              }         });        }  } 

jsonparser.java

package com.example.samplejsonparsing;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject;  import android.util.log;  public class jsonparser {      static inputstream = null;     static jsonobject jobj = null;     static string json = "";      // constructor     public jsonparser() {      }      public jsonobject 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 jsonobject(json);         } catch (jsonexception e) {             log.e("json parser", "error parsing data " + e.tostring());         }          // return json string         return jobj;      } } 

singlemenuitemactivity.java

package com.example.samplejsonparsing;  import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.textview;  public class singlemenuitemactivity  extends activity {      // json node keys     private static final string tag_name = "name";     private static final string tag_email = "email";     private static final string tag_phone_mobile = "mobile";     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.single_list_item);          // getting intent data         intent in = getintent();          // json values previous intent         string name = in.getstringextra(tag_name);         string cost = in.getstringextra(tag_email);         string description = in.getstringextra(tag_phone_mobile);          // displaying values on screen         textview lblname = (textview) findviewbyid(r.id.name_label);         textview lblcost = (textview) findviewbyid(r.id.email_label);         textview lbldesc = (textview) findviewbyid(r.id.mobile_label);          lblname.settext(name);         lblcost.settext(cost);         lbldesc.settext(description);     } } 

maybe can silly question please don't ignore this.

08-08 10:18:50.240: d/abslistview(14239): motionrecognitionmanager 08-08 10:18:50.285: d/androidruntime(14239): shutting down vm 08-08 10:18:50.285: w/dalvikvm(14239): threadid=1: thread exiting uncaught exception (group=0x40ee32a0) 08-08 10:18:50.295: e/androidruntime(14239): fatal exception: main 08-08 10:18:50.295: e/androidruntime(14239): java.lang.runtimeexception: unable start activity componentinfo{com.example.samplejsonparsing/com.example.samplejsonparsing.androidjsonparsingactivity}: android.os.networkonmainthreadexception 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2100) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2125) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activitythread.access$600(activitythread.java:140) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.os.handler.dispatchmessage(handler.java:99) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.os.looper.loop(looper.java:137) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activitythread.main(activitythread.java:4898) 08-08 10:18:50.295: e/androidruntime(14239):    @ java.lang.reflect.method.invokenative(native method) 08-08 10:18:50.295: e/androidruntime(14239):    @ java.lang.reflect.method.invoke(method.java:511) 08-08 10:18:50.295: e/androidruntime(14239):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 08-08 10:18:50.295: e/androidruntime(14239):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 08-08 10:18:50.295: e/androidruntime(14239):    @ dalvik.system.nativestart.main(native method) 08-08 10:18:50.295: e/androidruntime(14239): caused by: android.os.networkonmainthreadexception 08-08 10:18:50.295: e/androidruntime(14239):    @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1118) 08-08 10:18:50.295: e/androidruntime(14239):    @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 08-08 10:18:50.295: e/androidruntime(14239):    @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 08-08 10:18:50.295: e/androidruntime(14239):    @ java.net.inetaddress.getallbyname(inetaddress.java:214) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:670) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:509) 08-08 10:18:50.295: e/androidruntime(14239):    @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 08-08 10:18:50.295: e/androidruntime(14239):    @ com.example.samplejsonparsing.jsonparser.getjsonfromurl(jsonparser.java:38) 08-08 10:18:50.295: e/androidruntime(14239):    @ com.example.samplejsonparsing.androidjsonparsingactivity.oncreate(androidjsonparsingactivity.java:54) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activity.performcreate(activity.java:5206) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1083) 08-08 10:18:50.295: e/androidruntime(14239):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2064) 08-08 10:18:50.295: e/androidruntime(14239):    ... 11 more 08-08 10:19:39.280: i/process(14239): sending signal. pid: 14239 sig: 9 

androidmanifest

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.samplejsonparsing"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk         android:minsdkversion="8"         android:targetsdkversion="17" />      <uses-permission android:name="android.permission.internet"/>      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.example.samplejsonparsing.androidjsonparsingactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>         <activity             android:label="@string/app_name"             android:name="com.example.samplejsonparsing.singlemenuitemactivity" >         </activity>     </application>  </manifest> 

i have downloaded project , compiled on system. working fine. please check manifest.xml if have added "internet" permission. please add logcat output here 1 can understand error.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -