【发布时间】: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( )); }