【问题标题】:PubNub Server does not format message properlyPubNub 服务器未正确格式化消息
【发布时间】:2015-11-03 02:07:44
【问题描述】:

我的服务器配置可以与 Android 客户端对话:

<?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 标记在 Constants 类中很简单:

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";

如何重新配置​​服务器发送以允许应用内解析成功?

【问题讨论】:

  • 设备上的响应(消息)仍然只是一个字符串。我只需要将它转换为 JSONObject jsonObj = new JSONObject(message) 以使其成为 JSONObject 的实例

标签: php android json pubnub


【解决方案1】:

通过 PubNub 发送 JSON

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)));

请评论是否解决。

【讨论】:

    猜你喜欢
    • 2015-07-28
    • 2020-01-10
    • 2011-10-24
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多