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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

官网: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));

查看结果:


3 个回复

倒序浏览
回复 使用道具 举报
棒棒哒
回复 使用道具 举报
这个可以的嘛!!!哈哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马