android - Cannot use json to get the data from url (NullPointerException) -
i'm trying data json format of http://projectxapp.herokuapp.com/dashboard/ads/api/v1/ads/
it shows nullpointerexception, , i've tried variable ways solve it. still have no ideas!! seems objectlist empty.
public class imagepageractivity extends activity { // url make request private static string url = "http://projectxapp.herokuapp.com/dashboard/ads/api/v1/ads/"; // json node names static final string tag_meta = "meta"; static final string tag_meta_limit = "limit"; private static final string tag_meta_next = "next"; private static final string tag_meta_total_count = "count"; private static final string tag_objects = "objects"; private static final string tag_objects_end_time = "endtime"; private static final string tag_objects_id = "id"; private static final string tag_objects_link = "link"; private static final string tag_objects_photo = "photo"; private static final string tag_objects_photo_height = "height"; static final string tag_objects_photo_resource_uri = "resourceuri"; private static final string tag_objects_photo_source = "source"; private static final string tag_objects_photo_width = "width"; private static final string tag_objects_resource_uri = "oresourceuri"; private static final string tag_objects_start_time = "starttime"; // contacts jsonarray jsonarray meta = null; jsonarray objects = null; // private static final string test_file_name = "universal image loader @#&=+-_.,!()~'%20.png"; private static final string state_position = "state_position"; imageloader imageloader = imageloader.getinstance(); displayimageoptions options; viewpager pager; @suppresswarnings("null") @suppresslint("newapi") public void oncreate(bundle savedinstancestate) { // strictmode.setthreadpolicy(new strictmode.threadpolicy.builder() .detectdiskreads().detectdiskwrites().detectnetwork() .penaltylog().build()); strictmode.setvmpolicy(new strictmode.vmpolicy.builder() .detectleakedsqlliteobjects().detectleakedclosableobjects() .penaltylog().penaltydeath().build()); super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); ; setcontentview(r.layout.mos); file testimageonsdcard = new file("/mnt/sdcard", test_file_name); if (!testimageonsdcard.exists()) { copytestimagetosdcard(testimageonsdcard); } arraylist<hashmap<string, string>> objectslist = new arraylist<hashmap<string, string>>(); jsonparser jparser = new jsonparser(); jsonobject json = null; try { json = jparser.getstringcontent(url); } catch (exception e1) { // todo auto-generated catch block e1.printstacktrace(); } try { // getting array of contacts objects = json.getjsonarray(tag_objects); // looping through contacts (int = 0; < objects.length(); i++) { jsonobject c = objects.getjsonobject(i); string link = c.getstring(tag_objects_link); jsonobject photo = c.getjsonobject(tag_objects_photo); string source = photo.getstring(tag_objects_photo_source); hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_objects_link, link); map.put(tag_objects_photo_source, source); // adding hashlist arraylist objectslist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } string[] imageurls = null; if(objectslist.isempty()); toast.maketext(this, "no data", toast.length_long).show(); log.i("getdata1", "!!!!!!!!!!!"); // for(int i=0; i<objects.length(); i++) { log.i("getdata2", "!!!!!!!!!!!"); imageurls[i]=objectslist.get(i).get(tag_objects_photo_resource_uri); log.i("getdata3", "!!!!!!!!!!!"); } // private void copytestimagetosdcard(final file testimageonsdcard) { new thread(new runnable() { @override public void run() { try { inputstream = getassets().open(test_file_name); fileoutputstream fos = new fileoutputstream( testimageonsdcard); byte[] buffer = new byte[8192]; int read; try { while ((read = is.read(buffer)) != -1) { fos.write(buffer, 0, read); } } { fos.flush(); fos.close(); is.close(); } } catch (ioexception e) { l.w("can't copy test image onto sd card"); } } }).start(); }
there jsonparser().
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public static jsonobject getstringcontent(string uri) throws exception { try { httpclient client = new defaulthttpclient(); httpget request = new httpget(); request.seturi(new uri(uri)); httpresponse response = client.execute(request); inputstream ips = response.getentity().getcontent(); bufferedreader buf = new bufferedreader(new inputstreamreader(ips,"utf-8")); stringbuilder sb = new stringbuilder(); string s; while(true ) { s = buf.readline(); if(s==null || s.length()==0) break; sb.append(s); } buf.close(); ips.close(); jobj = new jsonobject(sb.tostring()); return jobj; } }
these logcat messages below!
04-05 07:25:17.889: e/strictmode(630): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 04-05 07:25:17.889: e/strictmode(630): @ java.lang.thread.run(thread.java:856) 04-05 07:25:21.831: e/androidruntime(3285): fatal exception: main 04-05 07:25:21.831: e/androidruntime(3285): java.lang.runtimeexception: unable start activity componentinfo{com.sai.mosorder/com.sai.mosorder.imagepageractivity}: java.lang.nullpointerexception 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activitythread.access$600(activitythread.java:141) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 04-05 07:25:21.831: e/androidruntime(3285): @ android.os.handler.dispatchmessage(handler.java:99) 04-05 07:25:21.831: e/androidruntime(3285): @ android.os.looper.loop(looper.java:137) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activitythread.main(activitythread.java:5041) 04-05 07:25:21.831: e/androidruntime(3285): @ java.lang.reflect.method.invokenative(native method) 04-05 07:25:21.831: e/androidruntime(3285): @ java.lang.reflect.method.invoke(method.java:511) 04-05 07:25:21.831: e/androidruntime(3285): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-05 07:25:21.831: e/androidruntime(3285): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-05 07:25:21.831: e/androidruntime(3285): @ dalvik.system.nativestart.main(native method) 04-05 07:25:21.831: e/androidruntime(3285): caused by: java.lang.nullpointerexception 04-05 07:25:21.831: e/androidruntime(3285): @ com.sai.mosorder.imagepageractivity.oncreate(imagepageractivity.java:132) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activity.performcreate(activity.java:5104) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 04-05 07:25:21.831: e/androidruntime(3285): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 04-05 07:25:21.831: e/androidruntime(3285): ... 11 more 04-05 07:25:22.200: e/surfaceflinger(37): ro.sf.lcd_density must defined build property 04-05 07:25:22.560: e/surfaceflinger(37): ro.sf.lcd_density must defined build property 04-05 07:26:07.869: e/activitythread(630): @ com.android.exchange.exchangeservice$7.run(exchangeservice.java:1856)
i found answer!!!! if confusing using json on android. must check website.
http://mobiforge.com/developing/story/consuming-json-services-android-apps
Comments
Post a Comment