黑马程序员技术交流社区

标题: 【上海校区】【区块链】以太坊 web3j for java 配置和使用 <1> [打印本页]

作者: 不二晨    时间: 2018-9-3 09:35
标题: 【上海校区】【区块链】以太坊 web3j for java 配置和使用 <1>
【转载】        https://blog.csdn.net/loy_184548/article/details/78630993
以太坊 web3j for java 配置和使用 <1>

官网:here

环境: mac & android studio

一、 导入包在app/build.gradle中:compile 'org.web3j:core:3.0.1-android'

即:

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')}

然后sync一下。

二、启动私有链

搭建: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三、初步尝试Web3j web3 = Web3jFactory.build(new HttpService());Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();String clientVersion = web3ClientVersion.getWeb3ClientVersion();

可能出现错误:

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...

解决方案:

运行成功,在调用的时候可能出现的错误:

    Failed to connect to localhost/127.0.0.1:8548

原因:模拟器默认把127.0.0.1和localhost当做本身了,在模拟器上可以用10.0.2.2代替127.0.0.1和localhost

解决办法:设置访问的ip为10.0.2.2

String url = "http://10.0.2.2:8545";Web3j web3 = Web3jFactory.build(new HttpService(url));

查看结果:




作者: 不二晨    时间: 2018-9-6 11:44
奈斯




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2