@Value("${wallet.net}")
private String walletNet;
@Value("${wallet.contractaddress}")
private String contractAddress;
@Value("${wallet.urlend}")
private String urlEnd;
上边是从配置文件获取一下地址。
public List<WalletUrlAmountInfo> getWalletUrlAmount(int userId)
throws Exception {
TBaseUser user = tBaseUserMapper.selectByPrimaryKey(userId);
String walletUrl = walletNet + "&" + contractAddress + "&adrees="
+ user.getCandyWalletAddress() + "&" + urlEnd;
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL getwalletAmountUrl = new URL(walletUrl);
System.out.println(walletUrl);
//打开连接,获取返回信息。
URLConnection context = getwalletAmountUrl.openConnection();
in = new BufferedReader(new InputStreamReader(
context.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result);
result = result.substring(result.indexOf("["),
result.indexOf("]") + 1);
System.out.println(result);
//json与对象之间的转换
JSONArray jsonArray = JSONArray.fromObject(result);
System.out.println(jsonArray);
List<WalletUrlAmountInfo> array = new ArrayList<WalletUrlAmountInfo>();
for (int i = 0; i < jsonArray.toArray().length; i++) {
Object o = jsonArray.get(i);
JSONObject jsonObject2 = JSONObject.fromObject(o);
WalletUrlAmountInfo walletUrlAmountInfo = (WalletUrlAmountInfo) JSONObject
.toBean(jsonObject2, WalletUrlAmountInfo.class);
array.add(walletUrlAmountInfo);
}
return array;
} catch (Exception e) {
throw e;
}
}
json与对象转换需要的包: