NIMSDK-AOS  10.5.0
NetCallAttachment.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk.msg.attachment;
2 
3 import java.io.Serializable;
4 import java.util.LinkedList;
5 import java.util.List;
6 import org.json.JSONArray;
7 import org.json.JSONException;
8 import org.json.JSONObject;
9 
10 public class NetCallAttachment implements MsgAttachment {
11 
12  private int type;
13 
14  private String channelId;
15  private int status;
16 
17  private List<Duration> durations;
18 
19  public int getType() {
20  return type;
21  }
22 
23  public String getChannelId() {
24  return channelId;
25  }
26 
27  public int getStatus() {
28  return status;
29  }
30 
31  public List<Duration> getDurations() {
32  return durations;
33  }
34 
35  @Override
36  public String toJson(boolean send) {
37  JSONObject jsonObject = new JSONObject();
38  try {
39  jsonObject.put("type", type);
40  jsonObject.put("channelId", channelId);
41  jsonObject.put("status", status);
42 
43  JSONArray jsonArray = new JSONArray();
44  if(durations != null)
45  {
46  for (Duration duration : durations) {
47  jsonArray.put(duration.toJson());
48  }
49  }
50 
51  jsonObject.put("durations", jsonArray);
52  } catch (JSONException e) {
53  e.printStackTrace();
54  }
55 
56  return jsonObject.toString();
57  }
58 
59  public static NetCallAttachment fromJson(String jsonString) {
60  NetCallAttachment netCallAttachment = new NetCallAttachment();
61 
62  try {
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");
68  }
69  int status = jsonObject.optInt("status");
70 
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));
78  }
79  }
80  }
81 
82  netCallAttachment.type = type;
83  netCallAttachment.channelId = channelId;
84  netCallAttachment.status = status;
85  netCallAttachment.durations = durations;
86  } catch (JSONException e) {
87  e.printStackTrace();
88  }
89 
90  return netCallAttachment;
91  }
92 
93 
94  public static final class NetCallAttachmentBuilder {
95  private int type;
96  private String channelId;
97  private int status;
98  private List<Duration> durations = new LinkedList<>();
99 
100  public NetCallAttachmentBuilder() {
101  }
102 
103  public NetCallAttachmentBuilder withType(int type) {
104  this.type = type;
105  return this;
106  }
107 
108  public NetCallAttachmentBuilder withChannelId(String channelId) {
109  this.channelId = channelId;
110  return this;
111  }
112 
113  public NetCallAttachmentBuilder withStatus(int status) {
114  this.status = status;
115  return this;
116  }
117 
118  public NetCallAttachmentBuilder withDurations(List<Duration> durations) {
119  this.durations = durations;
120  return this;
121  }
122 
123  public NetCallAttachment build() {
124  NetCallAttachment netCallAttachment = new NetCallAttachment();
125  netCallAttachment.durations = this.durations;
126  netCallAttachment.type = this.type;
127  netCallAttachment.channelId = this.channelId;
128  netCallAttachment.status = this.status;
129  return netCallAttachment;
130  }
131  }
132 
133  public static class Duration implements Serializable {
134  private String accid;
135  private int duration;
136 
137  public String getAccid() {
138  return accid;
139  }
140 
141  public int getDuration() {
142  return duration;
143  }
144 
145  public JSONObject toJson() {
146  JSONObject durationJson = new JSONObject();
147  try {
148  durationJson.put("accid", accid);
149  durationJson.put("duration", duration);
150  } catch (JSONException e) {
151  e.printStackTrace();
152  }
153  return durationJson;
154  }
155 
156  public static Duration fromJson(JSONObject durationJson) {
157  Duration duration = new Duration();
158  duration.accid = durationJson.optString("accid");
159  duration.duration = durationJson.optInt("duration");
160  return duration;
161  }
162  }
163 }
String toJson(boolean send)
将消息附件序列化为字符串,存储到消息数据库或发送到服务器。
static NetCallAttachment fromJson(String jsonString)