我目前正在尝试使用 Oandas REST API 和 Java 并坚持使用它。也许很公平,因为我对 REST 毫无头绪……当我尝试在浏览器中打开 URL 时,我被要求打开一个包含我想要的信息的文件。但是当我尝试通过 Java 打开文件时,会抛出 java.net 异常(超时)。
线程“主”java.net.ConnectException 中的异常:连接超时:连接
这是我的代码:
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);
}
}
非常感谢任何指导!