我使用LiveQuery for Parse Android来监听事件,即创建对象。创建对象后,我想将其添加到列表的开头。听力部分似乎有效,但我不确定如何处理收到的JSON对象。特别是,我遵循本教程并使用Back4App作为主持人:https://docs.back4app.com/docs/android/live-query/
摇篮:
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.github.tgio:parse-livequery:1.0.3'
我有一个Async任务,它在一个单独的线程上运行网络操作,并且能够检索所需的JSON对象:
private class UpdateTask extends AsyncTask<String, String, String> {
protected String doInBackground(final String... string) {
Log.e(getClass().getName(), "ASYNC");
// Subscription
final Subscription sub = new BaseQuery.Builder("Message")
.build()
.subscribe();
Log.e(getClass().getName(), String.valueOf(sub.isSubscribed()));
sub.on(LiveQueryEvent.CREATE, new OnListener() {
@Override
public void on(final JSONObject object) {
Log.e(getClass().getName(), object.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println("JSONObject" + object.toString());
// {"op":"create","clientId":13,"requestId":0,"object":{"conversationObject":{"__type":"Pointer","className":"Conversation","objectId":"lZsj4dPCvb"},"messageText":"What's your phone number?","author":{"__type":"Pointer","className":"_User","objectId":"2E0bbd3oSD"},"createdAt":"2017-09-15T15:40:39.303Z","updatedAt":"2017-09-15T15:40:39.303Z","__type":"Object","className":"Message","objectId":"7A4RHttAEr"}}
// TODO: Take JSON object, convert to POJO, add to mMessages, notify data adapter
String mJsonString = object.toString();
JsonParser parser = new JsonParser();
JsonElement mJson = parser.parse(mJsonString);
Gson gson = new Gson();
Message object2 = gson.fromJson(mJson, Message.class);
mMessages.add(0, object2);
adapter.notifyDataSetChanged();
}
});
}
});
return null;
}
}
我在创建对象后调用此异步任务,只需:
message.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
// Listen for messages and update UI
new UpdateTask().execute();
}
});
我的消息类看起来像这样(我使用的是Kotlin,但不会产生任何影响):
@ParseClassName("Message")
class Message : ParseObject() {
/* Message Pointers */
val author by ParseDelegate<ParseObject>()
val contact by ParseDelegate<Contact>()
/* Message Content */
val messageText by ParseDelegate<String>()
}
使用JSON对象,我应该能够将其转换为我的本地对象的版本,然后将其附加到列表中,并且我已经显示了我的上述尝试,但该对象不会出现创建它之后。我还是要刷新。
更糟糕的是,每次我都会收到此错误:
09 - 16 12: 05: 13.907 10004 - 10004 / com.sourcey.materialloginexample E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.sourcey.materialloginexample, PID: 10004
java.lang.NullPointerException: Attempt to invoke interface method 'void java.util.List.add(int, java.lang.Object)'
on a null object reference
at com.sourcey.materiallogindemo.activity.MessageActivity$UpdateTask$1$1.run(MessageActivity.java: 213)
at android.os.Handler.handleCallback(Handler.java: 751)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 154)
at android.app.ActivityThread.main(ActivityThread.java: 6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 776)
我认为错误是本地对象不是正确地从JSON创建的,即,当我打印Log.e(getClass().toString(), object2.getMessageText());
时,我得到了:
09 - 16 12: 15: 31.741 20252 - 20252 / com.sourcey.materialloginexample E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.sourcey.materialloginexample, PID: 20252
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java: 236)
at com.sourcey.materiallogindemo.activity.MessageActivity$UpdateTask$1$1.run(MessageActivity.java: 213)
at android.os.Handler.handleCallback(Handler.java: 751)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 154)
at android.app.ActivityThread.main(ActivityThread.java: 6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 776)
对象调试: