URL的JSON序列化总是返回NULL

May*_*aya 5 java serialization android json deserialization

我有一个Web URL,该URL可应要求返回JSON格式的字符串

{"StockID":0,"LastTradePriceOnly":"494.92","ChangePercent":"0.48"}
Run Code Online (Sandbox Code Playgroud)

我正在使用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();
Run Code Online (Sandbox Code Playgroud)

reader.readLine()总是回来null

知道我在做什么错吗?

这是实际的JSON地址http://app.myallies.com/api/quote/goog

更新

尽管两个链接具有相同的服务器实现以生成JSON响应,但相同的代码在http://app.myallies.com/api/news上可以正常工作。

小智 2

看起来这就是它想要的用户代理。以下代码对我有用:

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

    }

}
Run Code Online (Sandbox Code Playgroud)