我已经构建了一个RetroFitService以返回一些对象。在MainActivity中,我通过简单的按钮点击来调用服务。我似乎正在获得某种对象,但我不觉得它实际上是从我指定的REST API返回的。它显示了在调试器,但它的属性是空的:Retrofit对象返回为空
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: Some object is returned but its properties are null
Log.e("RetrofitService", "Returned objects: " + post);
Log.e("RetrofitService", "Object Id: " + post.getObjectId());
mCardAdapter.addData(post);
} else {
Log.e("RetrofitService", "Object returned is null.");
}
}
});
});
}
服务:
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 GET \
-H "X-Parse-Application-Id: asdf" \
-H "X-Parse-REST-API-Key: asdf" \
https://parseapi.back4app.com/classes/Post*/
}
的卷曲工作得很好。我没有收到任何错误。可能会出现什么问题? ?难道我@GET
方法不正确莫名其妙`
完成,这里是的ServiceFactory类:
public class ServiceFactory {
/**
* Creates a retrofit service from an arbitrary class (clazz)
* @param clazz Java interface of the retrofit service
* @param endPoint REST endpoint url
* @return retrofit service with 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;
}
}
而且我的build.gradle,因为我知道有所有不同的不一致改装版本:
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类:
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;
}
卷曲响应:
> 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"}]}
最后更新:我改变RetrofitService
的onNext()
方法通入CardAdapter,尽管这里未示出和超过的范围题。
@Override
public final void onNext(PostResponse postResponse) {
if (postResponse != null) {
// TODO: Some object is returned but its properties are null
Log.e("RetrofitService", "Objects successfully added to 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 is null.");
}
}
好像你正在试图获取这是通过解析SDK创建的对象。确保Post类的数据结构与端点返回的内容匹配(例如,使用String)。 而顺便说一句,我会掩盖的问题的网址和凭据。 –
我用Post对象更新了我的问题。我真的没有看到它的问题... – santafebound
你能否也提供来自 的json响应curl -X GET -H“X-Parse-Application-Id:asdf”-H“X-Parse-REST-API -Key:asdf“https://parseapi.back4app.com/classes/Post –