java - Retrofit object returned as null -
i've built retrofitservice return objects. in mainactivity call service simple button click. seem getting kind of object, don't feel it's being returned rest api specified. shows in debugger attributes null:
bfetch.setonclicklistener(v -> { v.startanimation(animationutils.loadanimation(this, r.anim.image_click)); retrofitservice service = servicefactory.createretrofitservice(retrofitservice.class, retrofitservice.service_endpoint); service.getposts() .subscribeon(schedulers.newthread()) .observeon(androidschedulers.mainthread()) .subscribe(new subscriber < post > () { @override public final void oncompleted() { log.e("retrofitservice", "retrofit request completed!"); } @override public final void onerror(throwable e) { log.e("retrofitservice", e.getmessage()); } @override public final void onnext(post post) { if (post != null) { // todo: object returned properties null log.e("retrofitservice", "returned objects: " + post); log.e("retrofitservice", "object id: " + post.getobjectid()); mcardadapter.adddata(post); } else { log.e("retrofitservice", "object returned null."); } } }); }); }
service:
public interface retrofitservice { string service_endpoint = "https://parseapi.back4app.com/"; @headers({ "x-parse-application-id: asdf", "x-parse-rest-api-key: asdf" }) @get("/classes/post") observable < post > getposts(); /*curl -x \ -h "x-parse-application-id: asdf" \ -h "x-parse-rest-api-key: asdf" \ https://parseapi.back4app.com/classes/post*/ }
the curl works fine. i'm not getting errors. going wrong? @get
method incorrect somehow?`
for completion, here servicefactory class:
public class servicefactory { /** * creates retrofit service arbitrary class (clazz) * @param clazz java interface of retrofit service * @param endpoint rest endpoint url * @return retrofit service defined endpoint */ public static <t> t createretrofitservice(final class<t> clazz, final string endpoint) { final restadapter restadapter = new restadapter.builder() .setendpoint(endpoint) .build(); t service = restadapter.create(clazz); return service; } }
and build.gradle because i'm aware there inconsistencies across different retrofit versions:
dependencies { compile filetree(dir: 'libs', include: ['*.jar']) androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testcompile 'junit:junit:4.12' /* reactivex */ compile 'io.reactivex:rxjava:1.0.17' compile 'io.reactivex:rxandroid:0.23.0' /* retrofit */ compile 'com.squareup.retrofit:retrofit:1.9.0' /* okhttp3 */ compile 'com.squareup.okhttp3:okhttp:3.8.1' /* recylerview */ compile 'com.android.support:recyclerview-v7:25.3.1' /* cardview */ compile 'com.android.support:cardview-v7:25.3.1' /* parse */ compile 'com.parse:parse-android:1.13.0' }
post class:
public class post implements serializable { private static final string class_name = "post"; private string objectid; private string text; public post(string objectid) { this.setobjectid(objectid); } public static string getclassname() { return class_name; } public string getobjectid() { return objectid; } private void setobjectid(string objectid) { this.objectid = objectid; } public string gettext() { return text; } public void settext(string text) { this.text = text; }
curl response:
> https://parseapi.back4app.com/classes/post/ % total % received % xferd average speed time time time current dload upload total spent left speed 100 259 100 259 0 0 360 0 --:--:-- --:--:-- --:--:-- 395{"results":[{"objectid":"ktefgr1pft","text":"hello world.","createdat":"2017-08-14t14:07:52.826z","updatedat":"2017-08-14t14:07:52.826z"},{"objectid":"mmh8l9gjck","text":"hello?","createdat":"2017-08-14t15:19:01.515z","updatedat":"2017-08-14t15:19:03.743z"}]}
final update: changed onnext()
method of retrofitservice
pass cardadapter, although isn't shown here , surpasses scope of question.
@override public final void onnext(postresponse postresponse) { if (postresponse != null) { // todo: object returned properties null log.e("retrofitservice", "objects added recyclerview adapter."); log.e("retrofitservice", "returned objects: " + postresponse.getresults()); log.e("retrofitservice", "text " + postresponse.getresults().get(0).gettext()); mcardadapter.adddata(postresponse); // } else { log.e("retrofitservice", "object returned null."); } }
try use following class in retrofitservice
@get("/classes/post") observable <postresponse> getposts();
postresponse wrapper class
public class postresponse { private list<post> results; public list<post> getresults() { return results; } public void setresults(list<post> results) { this.results = results; } }
Comments
Post a Comment