我很难用这个......
我有一个数据从pubnub连接进入服务
public void Startpubnub() {
Toast.makeText(this, "PubnubService created...", Toast.LENGTH_LONG).show();
Log.i("PUBNUB", "PubnubService created...");
try {
pubnub.subscribe(new String[] {channel}, new Callback() {
public void connectCallback(String channel) {
notifyUser("CONNECT on channel:" + channel);
}
public void disconnectCallback(String channel) {
notifyUser("DISCONNECT on channel:" + channel);
}
*public void reconnectCallback(String channel) {
notifyUser("RECONNECT on channel:" + channel);
}
@**Override
public void successCallback(String channel, Object message) {
Log.i("tag","broadcast is sent!");
//notifyUser(channel + " " + message.toString());
Gson gson = new Gson();
String jsonMessage = gson.toJson(message);
System.out.printf("tag",jsonMessage);
JSONObject json = null;
try {
json = new JSONObject(jsonMessage);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.printf("tag",json);
String data = null;
try {
data = json.getString("data");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("tag",data);**
}*
02-06 03:32:58.752: W/System.err(2690): org.json.JSONException: No value for data
02-06 03:32:58.752:W / System.err(2690):at org.json.JSONObject.get(JSONObject.java:354) 02-06 03:32:58.762:W / System.err(2690):at org.json.JSONObject.getString(JSONObject.java:510) 02-06 03:32:58.762:W / System.err(2690):at tv.loqoo.FourthScreen.FourthScreenPushService $ 2.successCallback(FourthScreenPushService.java:120) 02-06 03:32:58.762:W / System.err(2690):at com.pubnub.api.PubnubCore $ 7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762:W / System.err(2690):at com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762:W / System.err(2690):at com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762:W / System.err(2690):at java.lang.Thread.run(Thread.java:856) 02-06 03:32:58.762:W / dalvikvm(2690):threadid = 24:线程退出未捕获异常(组= 0x418b6300) 02-06 03:32:58.762:W / System.err(2690):java.lang.NullPointerException:println需要一条消息 02-06 03:32:58.762:W / System.err(2690):at android.util.Log.println_native(Native Method) 02-06 03:32:58.762:W / System.err(2690):在android.util.Log.i(Log.java:159) 02-06 03:32:58.762:W / System.err(2690):at tv.loqoo.FourthScreen.FourthScreenPushService $ 2.successCallback(FourthScreenPushService.java:125) 02-06 03:32:58.762:W / System.err(2690):at com.pubnub.api.PubnubCore $ 7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762:W / System.err(2690):at com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762:W / System.err(2690):at com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762:W / System.err(2690):at java.lang.Thread.run(Thread.java:856)
在successCallback中我试图将传入的消息解析为jsonobject并将附加内容发送给广播接收者。
我很难理解如何解析...对象消息。
这是传入消息的格式:
5 info = pubnub.publish({
6 'channel' : 'somechannel',
7 'message' : {
8 'data' : 'Hello my World',
9 'data2' : 'yup',
10 'data3' : 'yupyup'
11 }
12 })
答案 0 :(得分:1)
尝试这样的事情:
import org.json.JSONObject;
public void callback(JSONObject data) {
try {
String dt = data.getString("data");
String dt = data.getString("data1");
} catch (Exception e) {}
}
答案 1 :(得分:1)
我最终使用这个...来自将json发送到广播接收器类的服务。它非常冗长。但是真的不明白如何实施@ bhagat的答案......
private void broadcastMessage(JSONObject message) {
String msg = null;
try {
msg = message.getString("action");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(msg);
intent.putExtra("message", message.toString());
sendBroadcast(intent);
}
class MessageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
try {
String m = msg.getData().getString("message");
Log.i("handleMessage", m);
JSONObject message = (JSONObject) new JSONTokener(m).nextValue();
broadcastMessage(message);
} catch (JSONException e) {
e.printStackTrace();
}
}
};
@Override
public void successCallback(String channel, Object message) {
Log.i("tag","broadcast is sent!");
notifyUser(channel + " " + message.toString());
Log.i("afterBroadcastisSent", message.toString());
try {
Message m = Message.obtain();
Bundle b = new Bundle();
b.putString("message", message.toString());
m.setData(b);
mMessageHandler.sendMessage(m);
} catch (Exception e) {
e.printStackTrace();
}
}