【问题标题】:Android JSONObject becomes null when trying to send尝试发送时,Android JSONObject 变为 null
【发布时间】:2017-01-11 03:48:15
【问题描述】:

当我尝试将 JSONObject 推送到数据流上时,我已经正确构建了 JSONObject,但随后我尝试将其发布到流中,它为空。

JSONObject position = new JSONObject();

        try {
            position.put("lat", location.getLatitude());
            position.put("lng", location.getLongitude());
        } catch (JSONException e) {
            System.out.println("json not work");
            e.printStackTrace();
        }
        System.out.println(position) //result: {"lat":37,"lng":-122}
        pubNub.publish().message(position)//never sent, if other type it works

【问题讨论】:

  • 从未发送过,如果其他类型有效??详细解释一下

标签: java android json stream


【解决方案1】:

试试pubNub.publish().message(position.toString());

【讨论】:

  • 如果对 JSON 进行字符串化,则必须在另一端进行解析。您应该始终按原样发布 JSON 对象。
  • .toString 确实有效,但为什么我必须转换它并在另一端提取? JSON 应该只是作为 JSON 发送,它在此服务的规范中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多