从URL中读取内容失败

时间:2015-06-10 18:00:48

标签: android buffer inputstream

我正在尝试从网页读取JSON并将其存储为字符串,但是在调试时它会在try语句中保持失败:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,“UTF-8”));

public String networkRequest() throws IOException {

    URL url = new URL("https://graph.facebook.com/me");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    try
    {
        InputStream inputStream = url.openStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        StringBuilder stringBuilder = new StringBuilder();
        int cp;
        while ((cp = bufferedReader.read()) != -1)
        {
            stringBuilder.append((char) cp);
        }

        return stringBuilder.toString();
    }
    catch(Exception ex)
    {
        return "nothing";
    }
}

因为失败到达捕获并返回“没有”,有没有人理解我做错了什么?

BTW:我的网络非常好!

1 个答案:

答案 0 :(得分:0)

大。这意味着您正在从UI线程执行此方法,这是不推荐的,并且android严格模式不允许这样做。因此有一个例外。请从后台线程执行该方法。