Bitcoin Forum
November 22, 2017, 01:41:09 PM *
News: Latest stable version of Bitcoin Core: 0.15.1  [Torrent].
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: RPC_JSON java example connecting to bitcoind  (Read 634 times)
jquiroga
Newbie
*
Offline Offline

Activity: 10



View Profile
September 11, 2012, 07:57:35 PM
 #1

I was going to post this to
https://bitcointalk.org/index.php?topic=417.0

but I am a newbie and cannot.

Here it is:

// START OF FILE ----------------------------------------------------------------------------------------------

import java.io.*;
import java.net.*;
import java.nio.*;
import java.lang.management.*;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.*;

public class
json_test {

public static void
main(String[] args){
   JSONObject obj = new JSONObject();
   obj.put("method", "getinfo");
   obj.put("id", new Integer(11));

   String btc_comm = obj.toString();

   try{
      Authenticator.setDefault(new BitCoinAuthenticator());

      URL url_bcd = new URL("http://localhost:8332/");
      URLConnection yc = url_bcd.openConnection();
      yc.setDoOutput(true);
      OutputStreamWriter wr = new OutputStreamWriter(yc.getOutputStream());
      wr.write(btc_comm);
      wr.flush();

      BufferedReader in = new BufferedReader(
                  new InputStreamReader(
                  yc.getInputStream()));
      String inputLine;

      while((inputLine = in.readLine()) != null){
         System.out.println(inputLine);
      }
      in.close();
   } catch (MalformedURLException ex) {
      System.out.println(ex);
   } catch (IOException ex) {
      System.out.println(ex);
   }
}

}

class BitCoinAuthenticator extends Authenticator {
    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        String promptString = getRequestingPrompt();
        String hostname = getRequestingHost();
        InetAddress ipaddr = getRequestingSite();
        int port = getRequestingPort();

        String username = "";  // i have no user name in /home/your_user/.bitcoin/bitcoin.conf
        String password = "none"; // rpcpassword=none in /home/your_user/.bitcoin/bitcoin.conf
        return new PasswordAuthentication(username, password.toCharArray());
    }
}


// END OF FILE ----------------------------------------------------------------------------------------------

I compile with this script:

# START OF FILE
rm ./json_test.class
PS_CLASS_PATH=./lib/JSON/*
javac -classpath $PS_CLASS_PATH json_test.java
echo "Type enter \n Thanks." | more -1
# END OF FILE

And run it with this script:

# START OF FILE
PS_CLASS_PATH=./lib/JSON/*
java -cp $PS_CLASS_PATH json_test
echo "Type enter \n Thanks." | more -1
# END OF FILE

Preconditions:

1)

json-simple-1.1.1.jar

file must be in ./lib/JSON
(you can google it).

2)

bitcoind must be running and you must be able to type the command

bitcoind getinfo

and get something like:

{
    "version" : 60300,
    "protocolversion" : 60001,
    "walletversion" : 60000,
    "balance" : 0.00000000,
    "blocks" : 198343,
    "connections" : 8,
    "proxy" : "",
    "difficulty" : 2694047.95295501,
    "testnet" : false,
    "keypoololdest" : 1346473056,
    "keypoolsize" : 101,
    "paytxfee" : 0.00000000,
    "errors" : ""
}

Hope it helps.

JLQ.
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!