All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bitcoin.blockchain.api.exchange.JSONReader Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.bitcoin.blockchain.api.exchange;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * Created by Jesion on 2015-05-21.
 */
public class JSONReader {

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy