准备工作
将最新版本的web3j加入到项目配置中
Maven
Java 8:
org.web3j
core
2.2.1
Android:
org.web3j
core-android
2.1.0
Gradle
Java 8:
compile ('org.web3j:core:2.2.1')
Android:
compile ('org.web3j:core-android:2.1.0')
启动客户端
如果已经启动客户端,则不需要再次启动。
使用geth脚步进行启动
$ geth --fast --cache=512 --rpcapi personal,db,eth,net,web3 --rpc --testnet
使用Parity启动
$ parity --chain testnet
使用Infura提供的免费客户端启动
Web3j web3 = Web3j.build(new InfuraHttpService("https://morden.infura.io/your-token"));
如果需要详细了解Infura的启动方式,可以阅读Using Infura with web3j。
请求发送
使用Future发送异步请求
Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/
Web3ClientVersion web3ClientVersion = web3.web3Client