android - JSON exception to parse photo_refrence from Google places api -
i have made json android application, show reference of photos, in list view, gives me error code is\
package com.example.jsonapp; import java.util.arraylist; import java.util.hashmap; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.app.listactivity; import android.app.progressdialog; import android.content.context; import android.os.asynctask; import android.os.bundle; import android.view.menu; import android.widget.listadapter; import android.widget.simpleadapter;
public class mainactivity extends listactivity { // url make request private static string url = "https://maps.googleapis.com/maps/api/place/nearbysearch /json?location=mylatandlong&radius=5000&types=restaurant&sensor=false&key=myownkey";
// json node names private static final string tag_results = "results"; private static final string tag_photos = "photos"; private static final string tag_refrence = "photo_reference"; jsonarray results = null; jsonarray photos = null; context c; // progress dialog progressdialog pdialog; arraylist<hashmap<string, string>> contactlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new laodphotos().execute(tag_refrence); //listview lv = getlistview(); }//the end of on create public class laodphotos extends asynctask<string, string, string>{ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("loading profile ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } @override protected string doinbackground(string... arg0) { contactlist = new arraylist<hashmap<string, string>>(); jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(url); try{ results = json.getjsonarray(tag_results); photos = json.getjsonarray(tag_photos); for(int i=0; i<results.length(); i++){ jsonobject c = results.getjsonobject(i); if(c != null){ for(int j=0; j<photos.length(); j++){ jsonobject pc = photos.getjsonobject(j); string photo = pc.getstring(tag_refrence); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_refrence, photo); contactlist.add(map); } } } }catch(jsonexception e){ e.printstacktrace(); } return null; } @override protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { listadapter adapter = new simpleadapter(c, contactlist, r.layout.list_item, new string[]{tag_refrence}, new int[]{r.id.photoref}); setlistadapter(adapter); }//end of run method });//end of runonuithread }//end of onpost method } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; }
}
the log cat\ e/androidruntime(23042): java.lang.nullpointerexception e/androidruntime(23042): @ android.widget.simpleadapter.(simpleadapter.java:85)
would 1 me know how make succeed, want have photo_reference able show image, first need know how have photo_reference?!
you forget initialize c
context instance activity context before passing simpleadapter.so pass activity context adapter as:
listadapter adapter = new simpleadapter(mainactivity.this, contactlist, r.layout.list_item, new string[]{tag_refrence}, new int[]{r.id.photoref}); mainactivity.this.setlistadapter(adapter);
and onpostexecute
method run on ui thread no need use runonuithread
updating ui elements form onpostexecute
Comments
Post a Comment