1

这个我很难受...

我有数据从 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): 在 org.json.JSONObject.get(JSONObject.java:354) 02-06 03:32:58.762: W/System.err(2690) : 在 org.json.JSONObject.getString(JSONObject.java:510) 02-06 03:32:58.762: W/System.err(2690): 在 tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService.java: 120) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.PubnubCore$7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762: W/System .err(2690): 在 com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.Worker。运行(RequestManager.java:81)02-06 03:32:58.762:W/System.err(2690):在 java.lang.Thread.run(Thread.java:856)02-06 03:32:58.762: W/dalvikvm(2690): threadid=24: 线程以未捕获的异常退出 (group=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):在 android.util.Log.println_native(本机方法) 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): 在 tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService.java:125) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.PubnubCore$7。 handleResponse(PubnubCore.java:1483)02-06 03:32:58.762:W/System.err(2690):在 com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105)02-06 03:32: 58.762: W/System.err(2690): 在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 java.lang .Thread.run(Thread.java:856)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): 在 tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService .java:125) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.PubnubCore$7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api .Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 java.lang.Thread.run(Thread.java:856)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): 在 tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService .java:125) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.PubnubCore$7.handleResponse(PubnubCore.java:1483) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err(2690): 在 com.pubnub.api .Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 java.lang.Thread.run(Thread.java:856)在 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): 在 tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService.java:125) 02-06 03:32:58.762: W/System.err(2690): 在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): 在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System。错误(2690):在 java.lang.Thread.run(Thread.java:856)在 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): 在 tv.loqoo.FourthScreen.FourthScreenPushService$2.successCallback(FourthScreenPushService.java:125) 02-06 03:32:58.762: W/System.err(2690): 在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): 在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System。错误(2690):在 java.lang.Thread.run(Thread.java:856)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): 在 com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err (2690): 在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 java.lang.Thread.run(Thread.爪哇:856)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): 在 com.pubnub.api.SubscribeWorker.process(SubscribeWorker.java:105) 02-06 03:32:58.762: W/System.err (2690): 在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 java.lang.Thread.run(Thread.爪哇:856)在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 java.lang.Thread.run(Thread.java:856)在 com.pubnub.api.Worker.run(RequestManager.java:81) 02-06 03:32:58.762: W/System.err(2690): 在 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 })
4

2 回答 2

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();
            }
        }
于 2014-02-08T09:12:43.887 回答
1

尝试这样的事情:

import org.json.JSONObject;
public void callback(JSONObject data) {
    try {
        String dt = data.getString("data");
        String dt = data.getString("data1");
    } catch (Exception e) {}
}
于 2014-02-07T02:56:54.800 回答