【问题标题】:Android developer Json & PubnubAndroid 开发者 Json & Pubnub
【发布时间】:2014-03-22 17:25:52
【问题描述】:

您好,我写了一段代码来使用 Pubnub 频道发布一个 json:

pubnubMessage = new Pubnub("demo", "demo");     
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(id.map);
            map = mapFragment.getMap();
            LocationManager = new MyLocation(this);
            LocationManager.initLocation();
            callback = new LocationCallback() {
                @Override
                public void onNewLocation(Location location, String name) {
                    // *****sending the json string*****
                    JsonSendLocation jsonsendlocation = new JsonSendLocation();
                    jsonsendlocation.setLatitude(location.getLatitude());
                    jsonsendlocation.setLongtitude(location.getLongitude());
                    jsonsendlocation.setUsername(preferencesUtils.getName(
                            getApplicationContext(), preName, key));

                    Gson json = new Gson();
                    String jsonString = json.toJson(jsonsendlocation,
                            JsonSendLocation.class);

                    JSONObject ff;
                    try {
                        ff = new JSONObject(jsonString);
                        publishToPubnub(MY_CHANNEL, ff);                    
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

现在我想从我的 Pubnub 频道接收 json 对象,我写道:

Pubnub recieve = new Pubnub("demo", "demo");    
        try {
            recieve.subscribe("MyChannel", new com.pubnub.api.Callback() {

                @Override
                public void successCallback(String arg0, Object arg1) {
                    // TODO Auto-generated method stub

                }
            });
        } catch (PubnubException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

如何从我的频道获取我的 json?

【问题讨论】:

  • 我假设您问的是传递给 successCallback 参数的内容:字符串 arg0,对象 arg1。 Geremy 向您指出的文档有更清晰名称的参数,但仅供大家快速参考,如下所示:
  • public void successCallback(String channel, Object message) { System.out.println("SUBSCRIBE : " + channel + " : " + message.getClass() + " : " + message.toString( )); }

标签: android json gson pubnub


【解决方案1】:

首先,您可以使用Pubnub 对象的一个​​实例,只需将其声明为实例变量并在onCreate 中实例化它。然后您可以使用同一个 Pubnub 对象发布和订阅。

private Pubnub mPubNub;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.mPubNub = new Pubnub(Constants.PUBLISH_KEY, Constants.SUBSCRIBE_KEY);
    subscribe();
    // publish();
}

现在,当您在successCallback 中接收对象时,Java 类型转换是完全有效的。我喜欢使用instanceof 进行检查,因此如果消息由于某种原因无效,我的应用程序不会崩溃。

public void subscribe(){
    Callback callbacks = new Callback() {
        @Override
        public void successCallback(String channel, Object message) {
            if (message instanceof JSONObject){
                JSONObject json = (JSONObject) message;
                // Handle JSON
            } else if (message instanceof String){
                String str = (String) message;
                // Handle String
            }
            Log.d("PUBNUB", "Channel: " + channel + " Msg: " + message.toString());
        }
    };
    try {
        this.mPubNub.subscribe("MyChannel", callbacks);
    } catch (PubnubException e) {
        e.printStackTrace();
    }
}

如果您有任何其他问题,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2016-07-28
    • 2021-11-03
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    相关资源
    最近更新 更多