1 package com.netease.nimlib.sdk.msg.attachment;
3 import org.json.JSONArray;
4 import org.json.JSONException;
5 import org.json.JSONObject;
7 import java.io.Serializable;
8 import java.util.LinkedList;
15 private long channelId;
19 private List<Duration> durations;
39 JSONObject jsonObject =
new JSONObject();
41 jsonObject.put(
"type", type);
42 jsonObject.put(
"channelId", channelId);
43 jsonObject.put(
"status", status);
45 JSONArray jsonArray =
new JSONArray();
46 for (Duration duration : durations) {
47 jsonArray.put(duration.toJson());
50 jsonObject.put(
"durations", jsonArray);
51 }
catch (JSONException e) {
55 return jsonObject.toString();
62 JSONObject jsonObject =
new JSONObject(jsonString);
63 int type = jsonObject.optInt(
"type");
64 long channelId = jsonObject.optLong(
"channelId");
65 int status = jsonObject.optInt(
"status");
67 List<Duration> durations =
new LinkedList<>();
68 JSONArray jsonArray = jsonObject.optJSONArray(
"durations");
69 if (jsonArray != null) {
70 for (
int i = 0; i < jsonArray.length(); i++) {
71 JSONObject durationJson = jsonArray.optJSONObject(i);
72 if (durationJson != null) {
73 durations.add(Duration.fromJson(durationJson));
78 netCallAttachment.type = type;
79 netCallAttachment.channelId = channelId;
80 netCallAttachment.status = status;
81 netCallAttachment.durations = durations;
82 }
catch (JSONException e) {
86 return netCallAttachment;
90 public static final class NetCallAttachmentBuilder {
92 private long channelId;
94 private List<Duration> durations =
new LinkedList<>();
96 public NetCallAttachmentBuilder() {
99 public NetCallAttachmentBuilder withType(
int type) {
104 public NetCallAttachmentBuilder withChannelId(
long channelId) {
105 this.channelId = channelId;
109 public NetCallAttachmentBuilder withStatus(
int status) {
110 this.status = status;
114 public NetCallAttachmentBuilder withDurations(List<Duration> durations) {
115 this.durations = durations;
121 netCallAttachment.durations = this.durations;
122 netCallAttachment.type = this.type;
123 netCallAttachment.channelId = this.channelId;
124 netCallAttachment.status = this.status;
125 return netCallAttachment;
129 public static class Duration
implements Serializable {
130 private String accid;
131 private int duration;
133 public String getAccid() {
137 public int getDuration() {
141 public JSONObject
toJson() {
142 JSONObject durationJson =
new JSONObject();
144 durationJson.put(
"accid", accid);
145 durationJson.put(
"duration", duration);
146 }
catch (JSONException e) {
152 public static Duration
fromJson(JSONObject durationJson) {
153 Duration duration =
new Duration();
154 duration.accid = durationJson.optString(
"accid");
155 duration.duration = durationJson.optInt(
"duration");
String toJson(boolean send)
将消息附件序列化为字符串,存储到消息数据库或发送到服务器。
List< Duration > getDurations()
static NetCallAttachment fromJson(String jsonString)