2015-11-03 64 views
3

我有服務器配置爲Android的客戶說:PubNub Server不格式化消息正確

<?php 

require_once("mysql.class.php"); 
require_once("lib/autoloader.php"); 


// Setting up the PubNub Server: 
use Pubnub\Pubnub; 

$pubnub = new Pubnub(
    "pub-c...", ## PUBLISH_KEY 
    "sub-c..." ## SUBSCRIBE_KEY 
); 


// Publishing : 
$post_data = json_encode(array("type"=> "groupMessage", "data" => array("chatUser" => "SERVER", "chatMsg" => "Now lets talk", "chatTime"=>1446514201516))); 
$info = $pubnub->publish('MainChat', $post_data); 

print_r($info); 
print_r($post_data); 

?> 

和HTML:

<!doctype html> 
<html> 
<head> 
    <title>PubNub PHP Test Page</title> 
</head> 
    <body> 
     <form method="POST" action="index.php"> 
      <input type="submit" name="submit" value="TestSendMessage" /> 
     </form> 
    </body> 
</html> 

的發佈功能,服務器的工作,因爲我可以查看消息到達客戶端Android應用程序的日誌控制檯中,但消息從未解析正確,因此不會在給定SubscribeCallback的列表視圖中顯示:

public void subscribeWithPresence(String channel) { 
    this.channel = channel; 
    Callback subscribeCallback = new Callback() { 
     @Override 
     public void successCallback(String channel, Object message) { 
      if (message instanceof JSONObject) { 
       try { 
        JSONObject jsonObj = (JSONObject) message; 

        JSONObject json = jsonObj.getJSONObject("data"); 
        final String name = json.getString(Constants.JSON_USER); 
        final String msg = json.getString(Constants.JSON_MSG); 
        final long time = json.getLong(Constants.JSON_TIME); 
        if (name.equals(mPubNub.getUUID())) return; // Ignore own messages 
        final ChatMessage chatMsg = new ChatMessage(name, msg, time); 
        presentActivity.runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          // Adding messages published to the channel 
          mChatAdapter.addMessage(chatMsg); 
         } 
        }); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      Log.d("PUBNUB", "Channel: " + channel + " Msg: " + message.toString()); 

     } 

     @Override 
     public void connectCallback(String channel, Object message) { 
      Log.d("Subscribe", "Connected! " + message.toString()); 
      //hereNow(false); 
      // setStateLogin(); 
     } 
    }; 
    try { 
     mPubNub.subscribe(this.channel, subscribeCallback); 
     //presenceSubscribe(); 
    } catch (PubnubException e) { 
     e.printStackTrace(); 
     // Checking if success 
     Log.d("Fail subscribe ", "on channel: " + channel); 
    } 
} 

點擊TestSendMessage產量測試在瀏覽器中的服務器輸出:

Array ([0] => 1 [1] => Sent [2] => 14465159776373950) {"type":"groupMessage","data":{"chatUser":"SERVER","chatMsg":"Now lets talk","chatTime":1446514201516}} 

,並在應用程序從行日誌輸出:Log.d("PUBNUB", "Channel: " + channel + " Msg: " + message.toString());

返回:D/PUBNUB: Channel: MainChat Msg: {"type":"groupMessage","data":{"chatUser":"SERVER","chatMsg":"Now lets talk","chatTime":1446514201516}}

,因爲它應該,但消息從不出現在消息的ListView中,因此失敗了JSON解析。

的JSON標籤是從常量類也很簡單:

public static final String JSON_GROUP = "groupMessage"; 
public static final String JSON_USER = "chatUser"; 
public static final String JSON_MSG = "chatMsg"; 
public static final String JSON_TIME = "chatTime"; 

如何可以在服務器發送進行重新配置,以便在應用程序解析成功?

+0

設備上的響應(消息)仍然只是一個字符串。我只需要將其轉換爲JSONObject jsonObj = new JSONObject(message)使其成爲JSONObject的實例 – Sauron

回答

4

發送JSON在PubNub

Send the JSON object without stringifying it first。在PHP的情況下,請勿json_encode該消息。 PubNub SDK將爲您編碼和解碼。

此:

$post_data = array("type"=> "groupMessage", "data" => array(
    "chatUser" => "SERVER", "chatMsg" => "Now lets talk", 
    "chatTime"=>1446514201516)); 

不是這個:

$post_data = json_encode(array("type"=> "groupMessage", "data" => array(
    "chatUser" => "SERVER", "chatMsg" => "Now lets talk", 
    "chatTime"=>1446514201516))); 

請評論這是否解決與否。