1
\$\begingroup\$

I am writing a java program that will print current market values from a websites api. I used gson to parse the incoming json and Timer to run it every 5 minutes. I was wondering if this was the best approach. I will eventually have it update a server with this information.

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;



public class Main {
public static void main(String[] args) {
    Timer timer = new Timer();

    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            String requestURL = "https://bittrex.com/api/v1.1/public/getmarketsummaries";
            URL request;
            BufferedReader reader = null;
            try {
                request = new URL(requestURL);
                reader = new BufferedReader(new InputStreamReader(request.openStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }

            JsonReader jsonReader = new JsonReader(reader);
            JsonParser jsonParser = new JsonParser();
            JsonObject obj = jsonParser.parse(jsonReader).getAsJsonObject();
            JsonArray array = obj.getAsJsonArray("result");

            for (JsonElement t : array) {
                JsonObject obj2 = t.getAsJsonObject();

                System.out.println("Market Name: " + obj2.get("MarketName").getAsString());
                System.out.println("Last: " + obj2.get("Last").getAsBigDecimal());
                System.out.println("Base Volume: " + obj2.get("BaseVolume").getAsBigDecimal());
                //System.out.println(LocalDateTime.now());
            }



            //write your code here
            System.out.print(new Date() + "\n");
        }
    }, 0, 300000);
}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.