android app通过Geth RPC接口实现远程调用

记录一下APP怎么通过以太坊的RPC接口实现远程调用。
此过程的环境为Window7和Android studio2.2.3。以web3_clientVersion为例。更多用法参考Geth JSON-RPCManagement APIs
首先启动geth私有链并设定RPC端口--rpcport 8545

geth --identity "blossom"  --rpc  --rpccorsdomain "*" --datadir "%cd%" --port "30303"  --rpcapi "personal,db,eth,net,web3,miner"  --networkid 66666 --rpcport 8545 console 2>geth.txt

Android studio新建项目并添加.jar包。jar包下载

布局如下:

    <Button
        android:id="@+id/showView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="getBlock"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textview"/>

MainActivity如下:

package com.ethereum;

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

    @Override
    protected 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);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.showView) {
            show();
        }
    }

    private void show() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    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("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) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


}

添加权限:

<uses-permission  android:name="android.permission.INTERNET"/>

点击Button以后结果:
这里写图片描述

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页