php - PubNub 服务器未正确格式化消息

标签 php android json pubnub

我有服务器配置来与 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 的 ListView 中:

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

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

最佳答案

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

请评论是否解决了这个问题。

关于php - PubNub 服务器未正确格式化消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490282/

相关文章:

php - SQLSTATE[42S01] : Base table or view already exists or Base table or view already exists: 1050 Table

php - 使用 Google Analytics 跟踪 PHP API 的使用情况

Android Activity 垃圾收集

python - flatc 是否验证给定 JSON 的 Flatbuffer 模式的必填字段?

python - 在 Python 中使用其他 JSON 更改 JSON 值

返回空白的 PHP session 变量

php - 什么是 in_addr?

java - 如何在 Android 上用 C 加载我自己的 Java 类?

android - 如何将提示属性转换为 ViewModel 中的观察项

json - 类型 '{}' 不可分配给类型 'IntrinsicAttributes & IntrinsicClassAttributes