Ethereum web3j for java configuration and use <1>
Official website: here
Environment: mac & android studio
1. Import package
在app/build.gradle中:compile 'org.web3j:core:3.0.1-android'
- 1
- 2
which is:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'org.web3j:core:3.0.1-android'
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile files('libs/android-json-rpc-0.3.4.jar')
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
Then sync it.
2. Start the private chain
Build: here
启动:geth --rpcapi personal,db,eth,net,web3 --rpc --rinkeby console 2>>geth.log
//这里备注一下我的启动方式
geth --identity "linoy" --rpc --rpccorsdomain "*" --datadir "./" --port "30303" --nodiscover --rpcapi "personal,db,eth,net,web3,miner" --networkid 1999 console 2>>geth.log
查看:tail -f geth.log
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3. Preliminary attempt
Web3j web3 = Web3jFactory.build(new HttpService());
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
- 1
- 2
- 3
Errors may occur:
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
File1: /Users/pro/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.8.0/45b426f7796b741035581a176744d91090e2e6fb/jackson-annotations-2.8.0.jar
...
- 1
- 2
- 3
- 4
solution:
The operation is successful, the errors that may occur when calling:
Failed to connect to localhost/127.0.0.1:8548
- 1
- 2
Reason: The emulator regards 127.0.0.1 and localhost as itself by default. On the emulator, 10.0.2.2 can be used instead of 127.0.0.1 and localhost
Solution: set the access ip to 10.0.2.2
String url = "http://10.0.2.2:8545";
Web3j web3 = Web3jFactory.build(new HttpService(url));
- 1
- 2
- 3
View Results: