Oanda REST API和Java

时间:2013-06-13 12:04:45

标签: java json rest gson

我目前正在尝试使用Oandas REST API和Java,并坚持使用它。 也许是公平的,因为我对REST没有任何线索...... 当我尝试在浏览器中打开URL时,系统会要求我打开一个包含我想要的信息的文件。但是当我尝试通过Java打开文件时,会抛出java.net异常(超时)。

线程“main”中的异常java.net.ConnectException:连接超时:connect

这是我的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import com.google.gson.Gson;


public class Oanda {

private static String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuffer buffer = new StringBuffer();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1)
            buffer.append(chars, 0, read); 

        return buffer.toString();
    } finally {
        if (reader != null)
            reader.close();
    }
}
static class Prices {
    String instrument;
    String time;
    String bid;
    String ask;
}
/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("Downloading data...");
    String json = readUrl("http://api-sandbox.oanda.com/v1/instruments/price?instruments=EUR_USD");
    Gson gson = new Gson();        
    Prices prices = gson.fromJson(json, Prices.class);
    System.out.println(prices.instrument +": "+ prices.ask +" / "+ prices.bid);     
}


}

非常感谢任何指导!

0 个答案:

没有答案