import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
/*web3j import*/
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.utils.Convert;
import org.web3j.utils.Convert.Unit;
public String getBalanceToken(String addr) throws Exception
{
List lst = new ArrayList();
String url = "https://api.etherscan.io/api?module=account&action=balance&"
+ "apikey=이더스캔페이지에서 등록한 API KEY";
url += "&address=" + addr;
url += "&contractaddress=컨트렉트 ADDRESS";
Communicator comm = new Communicator();
String strJson = comm.getHttpJsonFromURL(url);
JSONObject jsonObj = new JSONObject(strJson);
String status = jsonObj.getString("status");
String rtnVal = "0.0";
if (status.equals("1"))
{
BigDecimal temp = new BigDecimal(jsonObj.getString("result"));
String formatString = "##,###,###,###,###,##0.0000000000000000";
Convert.Unit unit = Convert.Unit.ETHER;
String tempA = temp.toString();
BigDecimal tempB = Convert.fromWei(tempA, unit);
rtnVal = new DecimalFormat(formatString).format(tempB);
}
return rtnVal;
}
사용법 : getBalanceToken("지갑주소") 호출
* Communicator 클래스는 본 블로그의 자바 카테고리에서 다운로드 가능함