1 package com.netease.nimlib.sdk.msg.attachment;
3 import java.io.Serializable;
4 import java.util.LinkedList;
6 import org.json.JSONArray;
7 import org.json.JSONException;
8 import org.json.JSONObject;
14 private String channelId;
17 private List<Duration> durations;
37 JSONObject jsonObject =
new JSONObject();
39 jsonObject.put(
"type", type);
40 jsonObject.put(
"channelId", channelId);
41 jsonObject.put(
"status", status);
43 JSONArray jsonArray =
new JSONArray();
46 for (Duration duration : durations) {
47 jsonArray.put(duration.toJson());
51 jsonObject.put(
"durations", jsonArray);
52 }
catch (JSONException e) {
56 return jsonObject.toString();
63 JSONObject jsonObject =
new JSONObject(jsonString);
64 int type = jsonObject.optInt(
"type");
65 String channelId = null;
66 if(jsonObject.has(
"channelId")) {
67 channelId = jsonObject.optString(
"channelId");
69 int status = jsonObject.optInt(
"status");
71 List<Duration> durations =
new LinkedList<>();
72 JSONArray jsonArray = jsonObject.optJSONArray(
"durations");
73 if (jsonArray != null) {
74 for (
int i = 0; i < jsonArray.length(); i++) {
75 JSONObject durationJson = jsonArray.optJSONObject(i);
76 if (durationJson != null) {
77 durations.add(Duration.fromJson(durationJson));
82 netCallAttachment.type = type;
83 netCallAttachment.channelId = channelId;
84 netCallAttachment.status = status;
85 netCallAttachment.durations = durations;
86 }
catch (JSONException e) {
90 return netCallAttachment;
94 public static final class NetCallAttachmentBuilder {
96 private String channelId;
98 private List<Duration> durations =
new LinkedList<>();
100 public NetCallAttachmentBuilder() {
103 public NetCallAttachmentBuilder withType(
int type) {
108 public NetCallAttachmentBuilder withChannelId(String channelId) {
109 this.channelId = channelId;
113 public NetCallAttachmentBuilder withStatus(
int status) {
114 this.status = status;
118 public NetCallAttachmentBuilder withDurations(List<Duration> durations) {
119 this.durations = durations;
125 netCallAttachment.durations = this.durations;
126 netCallAttachment.type = this.type;
127 netCallAttachment.channelId = this.channelId;
128 netCallAttachment.status = this.status;
129 return netCallAttachment;
133 public static class Duration
implements Serializable {
134 private String accid;
135 private int duration;
137 public String getAccid() {
141 public int getDuration() {
145 public JSONObject
toJson() {
146 JSONObject durationJson =
new JSONObject();
148 durationJson.put(
"accid", accid);
149 durationJson.put(
"duration", duration);
150 }
catch (JSONException e) {
156 public static Duration
fromJson(JSONObject durationJson) {
157 Duration duration =
new Duration();
158 duration.accid = durationJson.optString(
"accid");
159 duration.duration = durationJson.optInt(
"duration");
String toJson(boolean send)
将消息附件序列化为字符串,存储到消息数据库或发送到服务器。
List< Duration > getDurations()
static NetCallAttachment fromJson(String jsonString)