【区块链】以太坊 web3j for java 配置和使用 <1>
2017-11-25 12:12
627 查看
以太坊 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));
查看结果:
相关文章推荐
- 【区块链】以太坊 web3j for java 使用 - 部署和调用合约 <3>
- 【区块链】以太坊 web3j for java 使用 - 为用户创建凭证credentials <2>
- Java使用synchronized实现多线程操作list<1>
- Java中使用通配符配置struts文件<单通配符和双通配符>
- java-mysql异常<Data truncation: Data too long for column 'name ' at row 1>讨论
- 【Java】多线程<1>-概念及Thread类基本使用
- ArcGIS server for java 安装配置<一>
- Selenium RC for JAVA 从零开始 <1>如何启动selenium server
- ArcGIS server for java 安装配置<一 续>
- 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true"
- [网络收集]回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证
- Java关键字final、static使用总结<转>
- 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/>
- 转:回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的
- Java导出pdf<1>
- java环境变量配置<转>
- 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/>
- 【引用】回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/>(转载)
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<httpModules> 节中包括..
- 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/>