コード例 #1
0
  private JSONObject getJSonFormMessage(Message msg) {
    String contentText = "";
    String msgType = ""; // 上传给js 层的类型,请和ios 保持一致

    switch (msg.getContentType()) {
      case text:
        contentText = ((TextContent) msg.getContent()).getText();
        msgType = "text";
        break;
      default:
        break;
    }
    Log.i(TAG, "msg " + contentText);

    JSONObject jsonItem = new JSONObject();
    try {
      MessageContent content = msg.getContent();
      UserInfo targetUser = (UserInfo) msg.getTargetInfo();
      UserInfo fromUser = (UserInfo) msg.getFromUser();

      jsonItem.put("target_type", "single");
      jsonItem.put("target_id", targetUser.getUserName());
      jsonItem.put("target_name", targetUser.getNickname());
      jsonItem.put("from_id", fromUser.getUserName());
      // jsonItem.put("from_name", fromUser.getNickname());
      jsonItem.put("from_name", msg.getFromName());
      jsonItem.put("create_time", msg.getCreateTime());
      jsonItem.put("msg_type", msgType);
      // jsonItem.put("text", contentText);

      JSONObject contentBody = new JSONObject();
      contentBody.put("text", contentText);
      jsonItem.put("msg_body", contentBody);

    } catch (JSONException e) {
      e.printStackTrace();
    }
    return jsonItem;
  }