如果我从前端执行此操作,则以下工作正常
var symbol = symbolsarray[i].trim();
var query = "select * from yahoo.finance.quotes where symbol = " + "'" + symbol + "'";
var yql = "http://query.yahooapis.com/v1/public/yql?q=" + escape(query) + "&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=?";
我已经尝试过这样从java这样做
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class JSONfunctions {
public static void main(String args[]) {
getQuote();
}
private static Double getQuote() {
try {
// URL url = new
// URL("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20%3D%20%27%255EHSI%27&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=jQuery1830024542473256587982_1445857302444");
String symbol = "%5EAORD";
String query = "select * from yahoo.finance.quotes where symbol = "
+ "'" + symbol + "'";
String yql = "http://query.yahooapis.com/v1/public/yql?q="
+ query
+ "&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=?";
URL url = new URL(yql);
BufferedReader reader = new BufferedReader(new InputStreamReader(
url.openStream()));
String line = reader.readLine();
System.out.println(line);
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
}
但我得到的回应是
Server returned HTTP response code: 400 for URL: http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol = '%5EAORD'&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=?
答案 0 :(得分:1)
尝试对您的查询进行编码,以便在网址中使用。
String yql = "http://query.yahooapis.com/v1/public/yql?q="
+ java.net.URLEncoder.encode(query, "UTF-8")
+ "&format=json&diagnostics=false&env=store://datatables.org/alltableswithkeys&callback=?";
您的查询包含空格,因此我怀疑这可能是一个解决方案。