How to post JSON using Retrofit 2 in android? -


i new android , dont know how post using retrofit

i have own server returns me data, need fetch one

this how url body looks enter image description here

i have send "city name" taken edittext , pass inside "ms_data's" keyword, i.e keyword="india"

this have tried far...

retrofit builder class

public class retrofitbuilder {  public static retrofit retrofit;  public static final string base_url = "my url";  public static retrofit getapidata() {      if(retrofit==null)     {         retrofit = new retrofit.builder().baseurl(base_url)                    .addconverterfactory(gsonconverterfactory.create()).build();     }     return retrofit; } } 

pojo class

public class messagefromserver {  private string city_name;  public messagefromserver(string city_name) {             this.city_name = city_name; }  public string getcity_name() {     return city_name; }  public void setcity_name(string city_name) {     this.city_name = city_name; }  @override public boolean equals(object o) {     if (this == o) return true;     if (o == null || getclass() != o.getclass()) return false;      messagefromserver = (messagefromserver) o;      return city_name != null ? city_name.equals(that.city_name) : that.city_name == null;  }  @override public int hashcode() {     return city_name != null ? city_name.hashcode() : 0; }  @override public string tostring() {     return "messagefromserver{" +             "city_name='" + city_name + '\'' +             '}'; } } 

requestbean class

public class jsonrequestbean {  private  string userid; private string sessiontoken; private string clienttype; private string msservicetype; private string msservice; private  string searchedloc = null; private list<string> msdata = null;     public list<string> getmsdata() {     return msdata; }  public void setmsdata(list<string> msdata) {     this.msdata = msdata; }   public jsonrequestbean() { }  public jsonrequestbean(string userid, string sessiontoken, string  clienttype, string msservicetype, string msservice, string  serachedloc) {     this.userid = userid;     this.sessiontoken = sessiontoken;     this.clienttype = clienttype;     this.msservicetype = msservicetype;     this.msservice = msservice;     this.searchedloc = serachedloc; }  public string getuserid() {     return userid; }  public void setuserid(string userid) {     this.userid = userid; }  public string getsessiontoken() {     return sessiontoken; }  public void setsessiontoken(string sessiontoken) {     this.sessiontoken = sessiontoken; }  public string getclienttype() {     return clienttype; }  public void setclienttype(string clienttype) {     this.clienttype = clienttype; }  public string getmsservicetype() {     return msservicetype; }  public void setmsservicetype(string msservicetype) {     this.msservicetype = msservicetype; }  public string getmsservice() {     return msservice; }  public void setmsservice(string msservice) {     this.msservice = msservice; }  public string getserachedloc() {     return searchedloc; }  public void setserachedloc(string serachedloc) {     this.searchedloc = serachedloc; }  @override public boolean equals(object o) {     if (this == o) return true;     if (o == null || getclass() != o.getclass()) return false;      jsonrequestbean = (jsonrequestbean) o;      if (userid != null ? !userid.equals(that.userid) : that.userid !=  null) return false;     if (sessiontoken != null ? !sessiontoken.equals(that.sessiontoken)  : that.sessiontoken != null)         return false;     if (clienttype != null ? !clienttype.equals(that.clienttype) :  that.clienttype != null)         return false;     if (msservicetype != null ?  !msservicetype.equals(that.msservicetype) : that.msservicetype !=  null)         return false;     if (msservice != null ? !msservice.equals(that.msservice) :  that.msservice != null)         return false;     return searchedloc != null ? searchedloc.equals(that.searchedloc)  : that.searchedloc == null;  }  @override public int hashcode() {     int result = userid != null ? userid.hashcode() : 0;     result = 31 * result + (sessiontoken != null ?  sessiontoken.hashcode() : 0);     result = 31 * result + (clienttype != null ? clienttype.hashcode()  : 0);     result = 31 * result + (msservicetype != null ?  msservicetype.hashcode() : 0);     result = 31 * result + (msservice != null ? msservice.hashcode() :  0);     result = 31 * result + (searchedloc != null ?  searchedloc.hashcode() : 0);     return result; }  @override public string tostring() {     return "jsonrequestbean{" +             "userid='" + userid + '\'' +             ", sessiontoken='" + sessiontoken + '\'' +             ", clienttype='" + clienttype + '\'' +             ", msservicetype='" + msservicetype + '\'' +             ", msservice='" + msservice + '\'' +             ", serachedloc=" + searchedloc +             '}'; }  private class msdata {      public string keyword;      public void setkeyword(string keyword) {         this.keyword = keyword;     } } } 

responsebean class //to fetch , store can use need

public class jsonresponsebean {  @serializedname("city_name") @expose public string cityname; @serializedname("id") @expose public string id;   public jsonresponsebean(string cityname, string id) {     this.cityname = cityname;     this.id = id; }  public void setcityname(string cityname) {     this.cityname = cityname; }  public string getcityname() {     return cityname; }  public string getid() {     return id; }  public jsonresponsebean() { }  @override public boolean equals(object o) {     if (this == o) return true;     if (o == null || getclass() != o.getclass()) return false;      jsonresponsebean = (jsonresponsebean) o;      if (cityname != null ? !cityname.equals(that.cityname) :   that.cityname != null)         return false;     return id != null ? id.equals(that.id) : that.id == null;  }  @override public int hashcode() {     int result = cityname != null ? cityname.hashcode() : 0;     result = 31 * result + (id != null ? id.hashcode() : 0);     return result; }   @override public string tostring() {     return "jsonrequestbean{" +             "cityname='" + cityname + '\'' +             ", id='" + id + '\'' +             '}'; } } 

//inner class, can see image // generated using jsonschema2pojo.org

 class errorcode  {     @serializedname("data")     @expose     public list<jsonresponsebean> data = null;     @serializedname("error_code")     @expose     public string errorcode;     @serializedname("message")     @expose     public string message;     @serializedname("status")     @expose     public string status;  } 

api class

public interface apirequest {  @headers({"content-type: application/json","accept:  application/json"}) @post("/lfs/city_name") call<jsonresponsebean> locdata (@body jsonrequestbean mrequest); } 

displayactivity actual data

// open activty, onfailure called

public class displayactivity extends appcompatactivity {   public static textview tvm,tvn; private button btnfetch; private edittext etcity; private static final string tag = "displayactivity"; private static final string value = "displaytest";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_display);      init();      intent = getintent();     string text = i.getstringextra("hesru");     tvn.settext(text);      string city_name = etcity.gettext().tostring();     final list<string> citylist = new arraylist<>() ;      citylist.add(city_name);     log.d(tag, "oncreate: city name-"+city_name);       btnfetch.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             retrofit retrofit = retrofitbuilder.getapidata();             apirequest service = retrofit.create(apirequest.class);              jsonrequestbean jsonrequestbean = new jsonrequestbean();             jsonrequestbean.setclienttype("mobile");             jsonrequestbean.setmsservicetype("search");             jsonrequestbean.setmsservice("locality");             jsonrequestbean.setmsdata(citylist);              call<jsonresponsebean> responsebeancall =  service.locdata(jsonrequestbean);             responsebeancall.enqueue(new callback<jsonresponsebean>()  {                 @override                 public void onresponse(call<jsonresponsebean> call,  response<jsonresponsebean> response) {                     log.d(tag, "onresponse:  "+response.issuccessful());                     log.d(tag, "onresponse:, responebody--- "+response.body());                 }                  @override                 public void onfailure(call<jsonresponsebean> call,  throwable t) {                     log.e(tag, "onfailure: message"+t.getmessage() );                     t.printstacktrace();                     toast.maketext(displayactivity.this, "something  went wrong", toast.length_short).show();                 }             });         }     });   } public void init() {     tvm = (textview) findviewbyid(r.id.textviewmsg);     tvn = (textview) findviewbyid(r.id.txtviewname);     btnfetch = (button) findviewbyid(r.id.buttonfetch);     etcity = (edittext) findviewbyid(r.id.etcity); } 

please solve this, found should use httpurlconnection, dont know how that.

follow these steps:

1.) firstly create modal class (myrequestclass) post request http://www.jsonschema2pojo.org/

2.) create modal (myresponseclass) server response same site.

3.) set values want send server creating object of myrequestclass.

4.) hit api using method in interface:

@post("/provider/filter") call<myresponseclass> senddatatoserver(@body myrequestclass mymodal); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -