A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【转载】        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'
  • 1
  • 2

即:

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

然后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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
三、初步尝试Web3j web3 = Web3jFactory.build(new HttpService());Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();String clientVersion = web3ClientVersion.getWeb3ClientVersion();
  • 1
  • 2
  • 3

可能出现错误:

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

解决方案:

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

    Failed to connect to localhost/127.0.0.1:8548
  • 1
  • 2

原因:模拟器默认把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));
  • 1
  • 2
  • 3

查看结果:



1 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马