Android附近的消息API发布'Expected BEGIN_OBJECT but was STRING'
Android Nearby Messages API Publish 'Expected BEGIN_OBJECT but was STRING'
我正在尝试使用附近的示例并遵循此 link:
https://developers.google.com/nearby/messages/android/pub-sub
我想订阅并发布一个小文本。
所以我补充说:
private Message mActiveMessage;
private void publish(String message) {
Log.i(TAG, "Publishing message: " + message);
mActiveMessage = new Message(message.getBytes());
Nearby.Messages.publish(mGoogleApiClient, mActiveMessage);
}
然后点击按钮:
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publish("Hello World");
}
});
并尝试在其他 phone onFound:
上接收它
public void onFound(final Message message) {
// Called when a new message is found.
mNearbyDevicesArrayAdapter.add(
DeviceMessage.fromNearbyMessage(message).getMessageBody());
String messageAsString = new String(message.getContent());
Log.d(TAG, "Found message: " + messageAsString);
Toast.makeText(getBaseContext(), messageAsString, Toast.LENGTH_LONG).show();
但我得到错误:
E/AndroidRuntime: FATAL EXCEPTION: main
gms.nearby.messages.samples.nearbydevices, PID: 733
com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was STRING at line 1
我做错了什么?
因为我认为我不需要像这里建议的那样去解析它:
GSON: Expected BEGIN_OBJECT but was STRING
另一个关于附近的问题:
可以只使用超声波到 public 文本吗?
感谢您的宝贵时间。
删除这一行:
mNearbyDevicesArrayAdapter.add(
DeviceMessage.fromNearbyMessage(message).getMessageBody());
DeviceMessage.fromNearbyMessage(message)
调用假定消息字节为 JSON 并尝试解析它,但您发布的消息不是 JSON。
我正在尝试使用附近的示例并遵循此 link: https://developers.google.com/nearby/messages/android/pub-sub
我想订阅并发布一个小文本。 所以我补充说:
private Message mActiveMessage;
private void publish(String message) {
Log.i(TAG, "Publishing message: " + message);
mActiveMessage = new Message(message.getBytes());
Nearby.Messages.publish(mGoogleApiClient, mActiveMessage);
}
然后点击按钮:
btnPublish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
publish("Hello World");
}
});
并尝试在其他 phone onFound:
上接收它public void onFound(final Message message) {
// Called when a new message is found.
mNearbyDevicesArrayAdapter.add(
DeviceMessage.fromNearbyMessage(message).getMessageBody());
String messageAsString = new String(message.getContent());
Log.d(TAG, "Found message: " + messageAsString);
Toast.makeText(getBaseContext(), messageAsString, Toast.LENGTH_LONG).show();
但我得到错误:
E/AndroidRuntime: FATAL EXCEPTION: main
gms.nearby.messages.samples.nearbydevices, PID: 733
com.google.gson.JsonSyntaxException:
java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was STRING at line 1
我做错了什么? 因为我认为我不需要像这里建议的那样去解析它: GSON: Expected BEGIN_OBJECT but was STRING
另一个关于附近的问题: 可以只使用超声波到 public 文本吗?
感谢您的宝贵时间。
删除这一行:
mNearbyDevicesArrayAdapter.add(
DeviceMessage.fromNearbyMessage(message).getMessageBody());
DeviceMessage.fromNearbyMessage(message)
调用假定消息字节为 JSON 并尝试解析它,但您发布的消息不是 JSON。