JSON 来自 URL 的序列化始终返回 NULL
JSON Serialisation from URL always returning NULL
我有一个网站 URL,其中 returns 一个 JSON 格式的字符串请求
{"StockID":0,"LastTradePriceOnly":"494.92","ChangePercent":"0.48"}
我正在使用 Java
进行流式传输
InputStream in = null;
in = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String result = sb.toString();
但是 reader.readLine()
总是返回 null
知道我做错了什么吗?
这是实际的 JSON 地址 http://app.myallies.com/api/quote/goog
更新
相同的代码在 http://app.myallies.com/api/news 上工作正常,尽管两个链接具有相同的服务器实现来生成 JSON 响应。
您得到 null 的原因是 Java 发出的请求与您的浏览器发出的请求不同。您的浏览器包含很多 headers 请求 Java 不会填充,除非您明确这样做。不过,我不确定为什么网络服务器不响应基本请求。您可以通过执行以下操作自行测试:
基本要求(无headers):
curl http://app.myallies.com/api/quote/goog
结果:null
请求headers:
curl \
-H "Host: app.myallies.com" \
-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0" \
-H "Accept: text/html,application/xhtml,application/xml;q=0.9,*/*;q=0.8" \
-H "Accept-Language: en-US,en;q=0.5" \
-H "Accept-Encoding: gzip, deflate" \
-H "Connection: keep-alive" \
-H "Cache-Control: max-age=0" \
http://app.myallies.com/api/quote/goog`
结果:{"StockID":0,"LastTradePriceOnly":"496.38","ChangePercent":"0.78"}
您可能需要设置一些请求 headers 以更接近地模仿浏览器发出的请求。
看起来它就是它想要的用户代理。以下代码对我有用:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class JSONTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://app.myallies.com/api/quote/goog");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0");
connection.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println(sb.toString());
}
}
我有一个网站 URL,其中 returns 一个 JSON 格式的字符串请求
{"StockID":0,"LastTradePriceOnly":"494.92","ChangePercent":"0.48"}
我正在使用 Java
进行流式传输InputStream in = null;
in = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String result = sb.toString();
但是 reader.readLine()
总是返回 null
知道我做错了什么吗?
这是实际的 JSON 地址 http://app.myallies.com/api/quote/goog
更新
相同的代码在 http://app.myallies.com/api/news 上工作正常,尽管两个链接具有相同的服务器实现来生成 JSON 响应。
您得到 null 的原因是 Java 发出的请求与您的浏览器发出的请求不同。您的浏览器包含很多 headers 请求 Java 不会填充,除非您明确这样做。不过,我不确定为什么网络服务器不响应基本请求。您可以通过执行以下操作自行测试:
基本要求(无headers):
curl http://app.myallies.com/api/quote/goog
结果:null
请求headers:
curl \
-H "Host: app.myallies.com" \
-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0" \
-H "Accept: text/html,application/xhtml,application/xml;q=0.9,*/*;q=0.8" \
-H "Accept-Language: en-US,en;q=0.5" \
-H "Accept-Encoding: gzip, deflate" \
-H "Connection: keep-alive" \
-H "Cache-Control: max-age=0" \
http://app.myallies.com/api/quote/goog`
结果:{"StockID":0,"LastTradePriceOnly":"496.38","ChangePercent":"0.78"}
您可能需要设置一些请求 headers 以更接近地模仿浏览器发出的请求。
看起来它就是它想要的用户代理。以下代码对我有用:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class JSONTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://app.myallies.com/api/quote/goog");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0");
connection.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println(sb.toString());
}
}