咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

参考web3j的响应类定义

平时写Android App和Web服务器的通讯是通过JSON的数据格式,然而自己对响应数据的定义的一般是使用Java Bean的继承来实现,下面是web3j的设计,也差不多这样,但是他的泛型用的挺的,以后可以参考。

比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
package org.web3j.protocol.core.methods.response;

import org.web3j.protocol.core.Response;

/**
* eth_coinbase.
*/
public class EthCoinbase extends Response<String> {
public String getAddress() {
return getResult();
}
}

Response类的 result 字段使用了泛型 T :

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.web3j.protocol.core;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* Our common JSON-RPC response type.
*
* @param <T> the object type contained within the response
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response<T> {
private long id;
private String jsonrpc;
private T result;
private Error error;
private String rawResponse;

public Response() {
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getJsonrpc() {
return jsonrpc;
}

public void setJsonrpc(String jsonrpc) {
this.jsonrpc = jsonrpc;
}

public T getResult() {
return result;
}

public void setResult(T result) {
this.result = result;
}

public Error getError() {
return error;
}

public void setError(Error error) {
this.error = error;
}

public boolean hasError() {
return error != null;
}

public String getRawResponse() {
return rawResponse;
}

public void setRawResponse(String rawResponse) {
this.rawResponse = rawResponse;
}

public static class Error {
private int code;
private String message;
private String data;

public Error() {
}

public Error(int code, String message) {
this.code = code;
this.message = message;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Error)) {
return false;
}

Error error = (Error) o;

if (getCode() != error.getCode()) {
return false;
}
if (getMessage() != null
? !getMessage().equals(error.getMessage()) : error.getMessage() != null) {
return false;
}
return getData() != null ? getData().equals(error.getData()) : error.getData() == null;
}

@Override
public int hashCode() {
int result = getCode();
result = 31 * result + (getMessage() != null ? getMessage().hashCode() : 0);
result = 31 * result + (getData() != null ? getData().hashCode() : 0);
return result;
}
}
}