android - Unable to map JSON to Model class using GSON -
i trying map json data downloaded google places api model class named "placessearchmodel.java" using gson library. unable access data inside model class. example checked "status" field using placessearchmodel.status or getstatus() method not show value, when checked using json response.getstring() method shows status value ok. problem either json gson mapping or model class.please me. below code:
placessearchmodel.java:
package com.example.admin.exploreme.model_classes; import java.util.list; import com.google.gson.annotations.expose; import com.google.gson.annotations.serializedname; public class placessearchmodel { @serializedname("html_attributions") @expose private list<object> htmlattributions = null; @serializedname("results") @expose private list<result> results = null; public string status = "hello"; public list<object> gethtmlattributions() { return htmlattributions; } public void sethtmlattributions(list<object> htmlattributions) { this.htmlattributions = htmlattributions; } public list<result> getresults() { return results; } public void setresults(list<result> results) { this.results = results; } public string getstatus() { return status; } public void setstatus(string status) { this.status = status; } } function download json data , map downloaded response model class"placessearchmodel" using gson:
public boolean callwebservice_for_placessearch(string item_clicked, double lati, double longi){ progressdialog.show(); dft.places_search_request(string.valueof(lati), string.valueof(longi), item_clicked, request.method.post, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response_received) { try{ string status = response_received.getstring("status"); log.d("<<status direct", status); }catch(exception e){ } log.d("<<response received", response_received.tostring()); try{ placessearchmodel = new placessearchmodel(); placessearchmodel = (placessearchmodel) dft.getresponseobject(response_received, placessearchmodel.class); log.d("<<status modelclass",placessearchmodel.status); // not showing value in logcat progressdialog.dismiss(); if(placessearchmodel.getstatus()=="zero_results"){ new alertdialog.builder(getactivity()) .setmessage("sorry no results found!") .setpositivebutton("ok", null) .show(); } else if(placessearchmodel.getstatus() == "ok"){ log.d("<<status","ok"); success = true; intent intent = new intent(getactivity(), listactivity.class); startactivity(intent); } } catch(exception e){ e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror volleyerror) { log.d("<< onerrorresponse", volleyerror.tostring()); } }); return success; } below function convert json gson object:
public object getresponseobject(jsonobject jsonresponse, class class1) { // todo auto-generated method stub object obj=null; obj = new gson().fromjson(jsonresponse.tostring(), class1); return obj; }
Comments
Post a Comment