Java获取json url禁用代码

时间:2014-12-13 01:40:25

标签: java json

我试图收到此网址的json:https://usecryptos.com/jsonapi/ticker/BTC-USD 它可以通过浏览器访问,但程序会给我以下错误:

    GRAVE: null
java.io.IOException: Server returned HTTP response code: 403 for URL: https://usecryptos.com/jsonapi/ticker/BTC-USD
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at java.net.URL.openStream(URL.java:1038)
    at main.JsonReader.readJsonFromUrl(JsonReader.java:28)
    at exchanges.brazil.UseCryptos.getTicker(UseCryptos.java:62)
    at exchanges.brazil.UseCryptos.get24hrVol(UseCryptos.java:50)
    at exchanges.brazil.UseCryptos.<init>(UseCryptos.java:42)
    at main.Main.init(Main.java:31)
    at main.Main.main(Main.java:52)

Exception in thread "main" java.lang.NullPointerException
    at exchanges.brazil.UseCryptos.getTicker(UseCryptos.java:73)
    at exchanges.brazil.UseCryptos.get24hrVol(UseCryptos.java:50)
    at exchanges.brazil.UseCryptos.<init>(UseCryptos.java:42)
    at main.Main.init(Main.java:31)
    at main.Main.main(Main.java:52)
Java Result: 1

我正在使用&#34; vol&#34;来调用此功能。和&#34; BTC_USD&#34;作为参数:

@Override
    public String getTicker(String info, String coin) throws JSONException{
        JSONObject json = null;
        try {
            String[] parts = coin.split("_");
            coin = parts[0]+"-"+parts[1];
            System.out.println(base_url + pairs_path_ticker[0][1] + coin);
            json = JsonReader.readJsonFromUrl(base_url + pairs_path_ticker[0][1] + coin);
            //json = json.getJSONObject("priVolume");

        } catch (IOException ex) {
            Logger.getLogger(UseCryptos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(UseCryptos.class.getName()).log(Level.SEVERE, null, ex);
        }
        switch (info) {

            case "vol":
                return Double.toString(json.getDouble("priVolume"));
            default:
                return null;
        }
    }

其他使用的功能:

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();
    }
  }

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我尝试使用curl下载你引用的https网址,它运行正常。我也尝试下载相同的网址,但我将https更改为http。我有一个html页面说&#34;对象移到这里&#34;这是&#34;在这里&#34;是https网址。所以可能发生的是你试图访问http网址而不是https网址。 https url确实包含JSON文件。

您可能需要使用&#34; -k&#34;卷曲后忽略证书
我还必须将json文件拆分为3行

    curl https://usecryptos.com/jsonapi/ticker/BTC-USD > x.json

    FILE: x.json

    {"id":113,"marketName":"BTC-USD","lastPrice":377.23420530,"last24hHigh":377.
    23420530,"last24hLow":377.23420530,"priVolume":0.00060138,"secVolume":0.
    22686111,"ask":347.83114848,"bid":337.55042488,"change":0.13968037854984894}

    curl http://usecryptos.com/jsonapi/ticker/BTC-USD > y.html

    File: y.html

    <html><head><title>Object moved</title></head><body> <h2>Object
    moved to <a
    href="https://usecryptos.com/jsonapi/ticker/BTC-USD">here</a>.</h2>
    </body></html>