Bitcoin Forum

Other => Beginners & Help => Topic started by: CharlyBln on June 28, 2011, 09:00:14 PM



Title: JAVA implementation for Mt. Gox API needed
Post by: CharlyBln on June 28, 2011, 09:00:14 PM
Hey Guys,

aside from the fact, that the Mt.Gox API seems to be not available for the moment i need your help.
Does anybody know an existing JAVA implementation for POST an https request on Mt.Gox?
I have experiences in programming with JAVA and i want to implement a simple "ticker" program, that only displays me current rates.
This whole displaying stuff and further functions will not be the problem, but my actual problem is getting the raw-data from Mt.Gox.
I would be so thankful if anybody has a short JAVA-example for me, which gives me some hints for solving this problem.


Title: Re: JAVA implementation for Mt. Gox API needed
Post by: CharlyBln on June 29, 2011, 08:10:17 AM
Is there nobody out there, who has something like this implemented? I need really only a code-fragment which grabs the ticker-data from Mt. Gox...


Title: Re: JAVA implementation for Mt. Gox API needed
Post by: SlipperySlope on June 29, 2011, 02:05:14 PM
Is there nobody out there, who has something like this implemented? I need really only a code-fragment which grabs the ticker-data from Mt. Gox...

I have not yet tried POST on Mt Gox.  Because their websocket API is non-responsive, I have been polling their published ticker API ...


Code:
    final String httpsURLString = "https://mtgox.com/code/data/ticker.php";
    final StringBuilder stringBuilder = new StringBuilder();
    try {
      final URL url = new URL(httpsURLString);
      final HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();
      final InputStream inputStream = httpURLConnection.getInputStream();
      final InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      final BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

      String inputLine;

      while ((inputLine = bufferedReader.readLine()) != null) {
        stringBuilder.append(inputLine);
        stringBuilder.append(' ');
      }

      bufferedReader.close();
    } catch (IOException ex) {
      // report exception
    }

    final JSONObject jsonObject1 = (JSONObject) JSONValue.parse(stringBuilder.toString());
    final JSONObject jsonObject2 = (JSONObject) jsonObject1.get("ticker");
    final BigDecimal bidPrice = new BigDecimal(jsonObject2.get("buy").toString());
    final BigDecimal askPrice = new BigDecimal(jsonObject2.get("sell").toString());
    final BigDecimal lastPrice = new BigDecimal(jsonObject2.get("last").toString());
    final BigDecimal highPrice = new BigDecimal(jsonObject2.get("high").toString());
    final BigDecimal lowPrice = new BigDecimal(jsonObject2.get("low").toString());
    final BigDecimal volume = new BigDecimal(jsonObject2.get("vol").toString());


The json-simple1.1.jar is a dependency.  I used keytool to import the Mt Gox SSL certificates from my web browser into cacerts, as I previously deleted all X509 certificates from that keystore as a security precaution.