-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRpcRequest.java
More file actions
100 lines (81 loc) · 2.56 KB
/
Copy pathRpcRequest.java
File metadata and controls
100 lines (81 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package rpc.json.message;
import org.json.JSONException;
import org.json.JSONObject;
import rpc.framework.message.RpcKey;
import rpc.framework.message.RpcMessage;
import rpc.util.RpcLog;
public class RpcRequest implements RpcMessage{
private static final String TAG = "RpcRequest";
private int mRequestId = 0; ///请求id
private String mMethod = null; ///请求方法名
private Object mParams = null; ///方法参数
public RpcRequest() {
}
public RpcRequest(int requestId, String method, Object params) {
mRequestId = requestId;
mParams = params;
mMethod = method;
}
@Override
public String toString() {
return "RpcRequest: "
+ "id=" + mRequestId
+ ",method=" + mMethod
+ ",\nparams=" + (mParams==null ? null : mParams.toString());
}
@Override
public int getId() {
return mRequestId;
}
public void setId(int requestId) {
mRequestId = requestId;
}
public Object getParams(){
return mParams;
}
public void setParams(Object params){
mParams = params;
}
public String getMethod(){
return mMethod;
}
public void setMethods(String method){
mMethod = method;
}
@Override
public String encode() {
JSONObject request = new JSONObject();
try {
request.put(RpcKey.METHOD, mMethod);
if (mParams == null) {
request.put(RpcKey.PARAMS, JSONObject.NULL);
} else {
request.put(RpcKey.PARAMS, mParams);
}
request.put(RpcKey.ID, mRequestId);
} catch (JSONException e) {
RpcLog.e(TAG, e);
}
return request.toString();
}
@Override
public void decode(String codes) {
JSONObject result = new JSONObject(codes);
try {
this.mMethod = result.getString(RpcKey.METHOD);
} catch (JSONException e) {
RpcLog.w(TAG , "JSONException: RpcRequest "+RpcKey.METHOD +" is not found!");
}
try {
this.mRequestId = result.getInt(RpcKey.ID);
} catch (JSONException e) {
RpcLog.w(TAG , "JSONException: RpcRequest "+RpcKey.ID+" is not found!");
}
try {
this.mParams = result.get(RpcKey.PARAMS);
} catch (JSONException e) {
RpcLog.w(TAG , "JSONException: RpcRequest "+RpcKey.PARAMS+" is not found!");
this.mParams = null;
}
}
}