javaappletprocessingprocessing-ide

How to use loadStrings in Processing/Java, when accessing remote server/other website?


I want to write a simple program in Processing, that would take data from here:

https://btc-e.com/api/2/btc_usd/trades

and display them in a chart. Let's consider the simplest example of accessing the data:

void setup() {
  size(400,400);
}

void draw() {
  background(0);
  fill(244);
  String[] t = loadStrings("https://btc-e.com/api/2/btc_usd/trades");
  text(t[0],100,100);
}

This works perfectly, when I run this as Java Application directly from Processing IDE (from both Processing 1.5 and 2.0). But then I export this as Java Applet (from Processing 1.5) I can not run this on either localhost or OpenProcessing. Java Machine runs, asks, whether I want to run the applet, I accept that, and then applet stays grey or white and nothing happens. What's the reason?

Is there any security problem, that Java Machine does not allow the code to get external data from other server? Is there any way to walk around the problem?

I stress, that I am working in Java/Java Applet mode, not in JavaScript, which I am sure does not allow such cross-source of data.


Solution

  • The data you're loading is a JSON formatted array so loadStrings won't be very useful in this case.

    You should be using loadJSONArray() and JSONObject to parse the data from each entry in the array you're loading.

    Here's a basic sample using just the amount values:

    void setup(){
      JSONArray data = loadJSONArray("https://btc-e.com/api/2/btc_usd/trades");//load the data
    
      for (int i = 0; i < data.size(); i++) {//traverse the data
    
        JSONObject entry = data.getJSONObject(i); //get each entry in the list/array
        //parse the values
        float amount = entry.getFloat("amount");
        int price    = entry.getInt("price");
        String item  = entry.getString("item");
        String pc    = entry.getString("price_currency");
        int tid      = entry.getInt("tid");
        int date     = entry.getInt("date");
        String tt    = entry.getString("trade_type");
    
        //do something with the data
        float x = (float)i/data.size() * width;
        float y = 100 - (amount * 20);
        line(x,height,x,y);
      }
    }
    

    And here's the output:

    chart

    Another note: in your code, you use loadStrings in the draw() loop which means you're loading the same data over and over again multiple times(about 60 by default) per second which is not a good idea. You should load the data once, have it available in a top level variable and reuse the loaded data in a draw() loop when you need it.

    Also, if you're loading external data you might need to sign the applet. Check out this guide. I've used the same guide to post this applet.