Retrofit returning null in android (API working and can get result in Advanced rest client ) -


i try user details of retrofit library, returns null, in advance rest client same input, working properly.

my apiclient

public class apiclient{   public static final string base_url = webconstants.common_url;   private static retrofit retrofit = null;    public static retrofit getclient() {     if (retrofit== null){        retrofit = new retrofit.builder()                  .baseurl(base_url)                  .addconverterfactory(gsonconverterfactory.create())                  .build();     }     return retrofit;   } } 

my apiinterface

@headers({"content-type: application/json"}) @post("user/getuserdetails") call<loginresponce> login(@body jsonobject data); 

and call like

private void logintask(jsonobject input){     apiinterfaces apiservice = apiclient.getclient().create(apiinterfaces.class);     call<loginresponce> login_call=apiservice.login(input);     login_call.enqueue(new callback<loginresponce>(){         @override         public void onresponce(call<loginresponce> call, responce<loginresponce> responce) {            error error = responce.body().geterror;            if(error.geterror_data() == 0){               user user = responce.body.getuser();            }else{            }         }         @override         public void onfailure(call<loginresponce> call, throwable t){         }      });    } } 

loginresponce model

public class loginresponce {      @serializedname("error")     private error error;      @serializedname("users")     private user user;      public error geterror(){ return error;}     public user getuser(){ return user;} } 

error , user models of same kind. in advanced rest client, it's content-type application/json.

you should not pass jsonobject. instead pass model object result.

your model object converted json , transmitted. can't directly pass jsonobject there.

for example, if sending username , password api. have send this.

your model:

public usermodel{     private string username;     private string password;  public string getusername() {     return username; }  public void setusername(string username) {     this.username = username; }  public string getpassword() {     return password; }  public void setpassword(string password) {     this.password = password; } } 

you have set data , call api this.

usermodel model = new usermodel(); model.setusername("username"); model.setpassword("password");  @headers({"content-type: application/json"}) @post("user/getuserdetails") call<loginresponce> login(@body usermodel data); 

hope helps:)


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -