java - NullPointerExceptions when parsing Json using Gson on Android -
i'm bit new posting on stackoverflow. i've done whole bunch of searching, , can't seem find answer helps me solve specific problem.
i trying parse particular json: https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=fuzzy%20monkey%27
i'm new json, , using google gson in order parse it. compiles fine. doesn't seem if java classes being populated proper info json (i keep getting nullpointerexceptions), due lack of knowledge json. in code, nullpointerexception comes final int n = response.results.size()
line main activity.
my main activity:
public class mainactivity extends actionbaractivity { public static inputstream json; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } responsedata response = new responsedata(); new jsonasync(this, response).execute(); final int n = response.results.size(); final textview[] mytextviews = new textview[n]; // create empty array; (int = 0; < n; i++) { // create new textview final textview textview = new textview(this); textview.settext("image name:" + response.results.get(i).titlenoformatting); relativelayout mylayout = (relativelayout) findviewbyid(r.id.container); // add textview linearlayout mylayout.addview(textview); // save reference textview later mytextviews[i] = textview; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } /** * async task grabbing json data * */ private static class jsonasync extends asynctask<void, void, responsedata> { private string text; public inputstream stream; private activity activity; private responsedata response; public jsonasync(activity activity, responsedata response) { this.activity = activity; this.response = response; } @override protected responsedata doinbackground(void...params) { try { url url = new url("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=fuzzy%20monkey%27"); inputstream stream = url.openstream(); gson gson = new gson(); string json = convertstreamtostring(stream); responsedata response = gson.fromjson(json, responsedata.class); return response; } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } @override protected void onpostexecute(responsedata result) { super.onpostexecute(result); } public static string convertstreamtostring(java.io.inputstream is) { java.util.scanner s = new java.util.scanner(is).usedelimiter("\\a"); return s.hasnext() ? s.next() : ""; } } }
my result class:
public class result { public int width; public int height; public string visibleurl; public string titlenoformatting; public url url; public result () { } }
my responsedata class:
public class responsedata { public list<result> results; }
i starting off basic, taking few values json put class , trying display names of image searches in dynamically created textviews (based on search count). have network permissions declared in manifest don't think it's network problem. appreciated!
you should put code depends on asynctask in onpostexecute.
@override protected void onpostexecute(responsedata result) { final int n = result.results.size(); final textview[] mytextviews = new textview[n]; // create empty array; (int = 0; < n; i++) { // create new textview final textview textview = new textview(mainactivity.this); textview.settext("image name:" + result.results.get(i).titlenoformatting); relativelayout mylayout = (relativelayout) findviewbyid(r.id.container); // add textview linearlayout mylayout.addview(textview); // save reference textview later mytextviews[i] = textview; } super.onpostexecute(result); }
edit: missed thing. wherever response.results use result.results instead. in 2 lines:
final int n = result.results.size();
and
textview.settext("image name:" + result.results.get(i).titlenoformatting);
if null again, maybe because name of class 'responsedata' , mark in json 'responsedata' not same (first letter).
Comments
Post a Comment