Several previous blogs mainly introduced the Go-ethereum client different environment, today this blog is based on the first few rows of blog. When the build is complete, we can interact with the nodes in a variety of ways (JavaScript Console, JSON-RPC, WEB3, etc.). This article is to create an account as an example, describes the Java invoke Json-rpc method and the process may encounter problems. Ready to
When you are ready to use a different API interface, when you start the node to add different parameters, such as you want to use RPC call to create an account (personal operation), then in the command to start to verify that there are –RPCAPI parameters, parameter values are personal options, Otherwise it cannot be called correctly:
--rpcapi "Db,eth,net,web3,personal,admin,miner"
API call
When the node is ready, and make sure the network is unblocked, here is a description of Java's specific code.
First, the JSON-RPC jar package is introduced in the Pom file:
<dependency>
<groupId>com.github.briandilley.jsonrpc4j</groupId>
<artifactId> jsonrpc4j</artifactid>
<version>1.4.6</version>
</dependency>
This dependency is passed into the Jackson-core, please note that this jar package introduces Jackson-core to conflict with Jackson-core versions of your system. I've stepped over the pit here because two versions cause all sorts of inexplicable problems. If there is more than one Jackson-core, specify which version is introduced explicitly.
Instance:
Import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
Import Java.net.URL;
/**
* @author Zhuzhisheng *
@Description
* @date on 2016/12/31.
*/Public
class Testjsonrpc {public
static void Main (string[] args) {
//TODO Multiple Parameters Use example
// string[ ] temp = new string[]{"0x12341234"}; object[] params = new object[]{"0x1", "0x2", "0X8888F1F195AFA192CFEE860698584C030F4C9DB1", temp};
The password is 123456
object[] params = new object[]{"123456"};
String methodname = "Personal_newaccount";
try {
Jsonrpchttpclient client = new Jsonrpchttpclient (New URL ("http://127.0.0.1:8545"));
Object address = Client.invoke (methodname, params, object.class);
SYSTEM.OUT.PRINTLN (address);
catch (Throwable throwable) {
throwable.printstacktrace ();}}}
Executes the main method, printing the address of the creation account. Calls from other methods can be extended on this basis. For multiple parameters, the reference to an array type is a piece of code commented out above. Attention Matters
(1) Jar pack conflict problem;
(2) Parameter array type and specific parameter type;
(3) Whether the time taken by the calling node to create the account can meet the requirements of the system;
ps:2016 the last day of the year, I wish you all a happy new year. There is a new harvest in the new year.