實現的業務邏輯是這樣的:
通過http請求,返回一個json格式的數據,然后將json數據轉化為java對象返回給調用方。
Http采用OkHttp庫,json轉化采用jackson庫。
一.簡介
1)okhttpclient
OkHttpClient官網: http://square.github.io/okhttp/
OkHttp GitHub地址:https://github.com/square/okhttp
最常用的是兩個http請求是get和post,我下面的代碼就只用到這兩個請求。
Maven依賴:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
2)jackson
Jackson的文檔:http://wiki.fasterxml.com/JacksonInFiveMinutes
Jackson的maven依賴
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-parent</artifactId>
<version>2.8</version>
</dependency>
由於采用了javabean的方式,所以json和java類的互轉變得簡單。
二.代碼
java類:
public class Transaction {
//使用了靜態的內部類
public static class Output{
String address;
long amount;
public String getAddress() {
return address;
}
public long getAmount() {
return amount;
}
public void setAddress(String address) {
this.address = address;
}
public void setAmount(long amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Output{" +
"address='" + address + '\'' +
", amount=" + amount +
'}';
}
}
private String txid;
private String action;
private long amount;
private long fees;
private long time;
private int confirmations;
private List<Output> outputs;
public void setTxid(String txid) {
this.txid = txid;
}
public void setAction(String action) {
this.action = action;
}
public void setAmount(long amount) {
this.amount = amount;
}
public void setFees(long fees) {
this.fees = fees;
}
public void setTime(long time) {
this.time = time;
}
public void setConfirmations(int confirmations) {
this.confirmations = confirmations;
}
public String getTxid() {
return txid;
}
public void setOutputs(List<Output> outputs) {
this.outputs = outputs;
}
@Override
public String toString() {
return "Transaction{" +
"txid='" + txid + '\'' +
", action='" + action + '\'' +
", amount=" + amount +
", fees=" + fees +
", time=" + time +
", confirmations=" + confirmations +
", outputs=" + outputs +
'}';
}
}
http請求:
public class HttpClient {
public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
public static String httpGet(String url) throws IOException {
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = httpClient.newCall(request).execute();
return response.body().string(); // 返回的是string 類型,json的mapper可以直接處理
}
public static String httpPost(String url, String json) throws IOException {
OkHttpClient httpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = httpClient.newCall(request).execute();
return response.body().string();
}
}
json轉化:
public Transaction[] getTransaction(int skip,int limit){
String txHistoryUrl = String.format("%s%s?skip=%d&limit=%d",url,TX_HISTORY,skip,limit); //url 后邊的參數是業務規定,格式可以參考,具體可以自定義
System.out.println(txHistoryUrl);
try{
String newTx = HttpClient.httpGet(txHistoryUrl);
System.out.println(newTx);
ObjectMapper mapper = new ObjectMapper(); // 只需要一個mapper就可以實現
return mapper.readValue(newTx,Transaction[].class);
}catch (IOException e){
e.printStackTrace();
}
return null;
}
json數據如下:
[{"txid":"ad416e1b4b8b807b4a0946affb17cf253c1af7a7e6c6a9e21fac2e93b2c88746","action":"received","amount":10000,"fees":3800,"time":1486111663,"confirmations":179,"outputs":[{"amount":10000,"address":"2MxDyD4idPv8LqYNP4Aq4QExRhTckqNK8zK"}]},{"txid":"2dadf7b391935af15f5f70e775ed5d7a03d629db4bdde1cd93e31b8311db4949","action":"received","amount":1000000,"fees":4416,"time":1485241194,"confirmations":1721,"outputs":[{"amount":1000000,"address":"2MxDyD4idPv8LqYNP4Aq4QExRhTckqNK8zK"}]}]
三.問題解決
Json錯誤:Can not deserialize instance of xx out of START_ARRAY token
Server
這個可能是后台返回的是數組類型的json,所以transaction要用數組
return mapper.readValue(newTx,Transaction[].class);
json錯誤:
No suitable constructor found for type [xxxx]: can not instantiate from JSON object (need to add/enable type information?) at xxx
這個需要java class中的內部類使用靜態類,見transaction類中的output內部類。
后續復雜應用,再接着寫。
送你幾顆比特幣玩玩:
