android - Realm Object is empty when posting with Retrofit -
i try pass realm object retrofit put method. although there no threading error unlike other, somehow retrofit passing empty object without data realm object. user extends realm object.
this retrofit method:
@headers("content-type: application/json") @put(user + "/{id}") observable<profile> edituser(@body user user, @path("id") string id);
this how call in activity:
if (!name.gettext().tostring().isempty() && !email.gettext().tostring().isempty() && !phone.gettext().tostring().isempty()) { realm.begintransaction(); agent.setname(name.gettext().tostring()); agent.setphone(phone.gettext().tostring()); agent.setemail(email.gettext().tostring()); realm.committransaction(); consumerrestadapter.getservice(this) .edituser(agent, agent.getid()) .subscribeon(schedulers.newthread()) .observeon(androidschedulers.mainthread()) .subscribe(new observer<profile>() { @override public void oncompleted() { fillvalues(); } @override public void onerror(throwable e) { timber.tag("profile editing onerror").e(e.getlocalizedmessage()); } @override public void onnext(profile profile) { realm.begintransaction(); realm.where(user.class).findall().clear(); realm.copytorealm(profile.getuser()); agent = profile.getuser(); realm.committransaction(); } }); }
but when checked retrofit log, object passed server empty object.
{"created_at":0,"has_password":false,"is_admin":false,"updated_at":0}
oh..i forgot intercepting gson header. here answer.
public restadapter normalrestadapter() { //let gson ignore realm object gson gson = new gsonbuilder().setexclusionstrategies(new exclusionstrategy() { @override public boolean shouldskipfield(fieldattributes f) { return f.getdeclaringclass().equals(realmobject.class); } @override public boolean shouldskipclass(class<?> clazz) { return false; } }).create(); //okhttp read cookie server okhttpclient.setcookiehandler( new cookiemanager(persistentcookiestore, cookiepolicy.accept_all)); restadapter restadapter; if (buildconfig.debug) { restadapter = new retrofit.restadapter.builder().setendpoint(base_url) .setconverter(new gsonconverter(gson)) .setloglevel(restadapter.loglevel.full) .setclient(new okclient(okhttpclient)) .setrequestinterceptor(new nnrequestinterceptor()) .seterrorhandler(new retrofiterrorhandler(mcontext)) .build(); } else { restadapter = new retrofit.restadapter.builder().setendpoint(base_url) .setconverter(new gsonconverter(gson)) .setclient(new okclient(okhttpclient)) .seterrorhandler(new retrofiterrorhandler(mcontext)) .build(); } return restadapter;
}
the solution created new user object , pass retrofit.
user user = new user();
and works now.
Comments
Post a Comment