Android通过Geth RPC接口实现接入以太坊私有链
生活随笔
收集整理的這篇文章主要介紹了
Android通过Geth RPC接口实现接入以太坊私有链
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
環(huán)境:mac os & android studio
一、啟動私有鏈
搭建方法見?here
啟動并設(shè)定RPC端口:
geth --identity "linoy" --rpc --rpccorsdomain "*" --datadir "./" --port "30303" --nodiscover --rpcapi "personal,db,eth,net,web3,miner" --networkid 1999 console 2>>geth.log- 1
- 2
需要注意的是rpcapi 參數(shù):
這個(gè)命令指示了允許通過RPC訪問的命令。默認(rèn)情況下,Geth允許web3。當(dāng)你準(zhǔn)備使用不同的api接口時(shí),在啟動節(jié)點(diǎn)時(shí)要添加上不同的參數(shù),比如你要使用rpc調(diào)用創(chuàng)建賬戶(personal操作),那么在啟動的命令中就需要核實(shí)是否有–rpcapi參數(shù),參數(shù)值中是否有personal選項(xiàng),否則無法正常調(diào)用- 1
- 2
- 3
- 4
二、Android studio新建項(xiàng)目并添加.jar包
下載地址:here
新建項(xiàng)目
選擇project
導(dǎo)入jar包
將你要導(dǎo)入的jar包拖入libs文件夾內(nèi),然后
三、設(shè)置布局文件
<Buttonandroid:id="@+id/showView"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="getBlock"/><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/textview"/>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
四、設(shè)置權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>- 1
五、主代碼
package com.example.pro.blockchain1;import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView;import org.alexd.jsonrpc.JSONRPCException; import org.alexd.jsonrpc.JSONRPCHttpClient;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private Button showButton;private TextView textview;public static final int SHOW = 0;private Handler handler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case SHOW:String a = (String) msg.obj;textview.setText(a);}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textview=(TextView)findViewById(R.id.textview);showButton = (Button) findViewById(R.id.showView);showButton.setOnClickListener(this);}@Overridepublic void onClick(View v) {if(v.getId() == R.id.showView) {show();}}private void show() {new Thread(new Runnable() {@Overridepublic void run() {try {String url = "http://10.0.2.2:8545"; //注意,不是127.0.0.1JSONRPCHttpClient client = new JSONRPCHttpClient(url);Map<String, Object> map = new HashMap();List paramsList = new ArrayList();map.put("json-rpc", "2.0");map.put("method", "web3_clientVersion");map.put("params", paramsList);map.put("id", "67");String st = client.callString("web3_clientVersion", map);Message msg = new Message();msg.what =SHOW;msg.obj = st;handler.sendMessage(msg);} catch (JSONRPCException e) {// textview.setText("step wrong??");e.printStackTrace();}}}).start();} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
可以看到結(jié)果:
其他事例
String url = "http://10.0.2.2:8545"; JSONRPCHttpClient client = new JSONRPCHttpClient(url); Map<String, Object> map = new HashMap(); List paramsList = new ArrayList(); map.put("id", "1"); map.put("json-rpc", "2.0"); map.put("params", paramsList);//personal_listAccounts String st = client.callString("personal_listAccounts", map);//personal_newAccount paramsList.add("123456"); String st = client.callString("personal_newAccount", map);- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
參考文檔:here?&?here
API文檔:here?&?here
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 http://blog.csdn.net/loy_184548/article/details/78515600總結(jié)
以上是生活随笔為你收集整理的Android通过Geth RPC接口实现接入以太坊私有链的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 以太坊源码学习 -- EVM
- 下一篇: EVM反编译软件Porosity的使用-