【问题标题】:Reading JSON data from the net (Twitter)从网络(Twitter)读取 JSON 数据
【发布时间】:2011-10-11 16:52:26
【问题描述】:

我找到了这篇关于如何使用 JSON 检索 Twitter 更新并将其发布到 TextView 的精彩教程: http://www.ibm.com/developerworks/xml/library/x-andbene1/

我是按照这个教程一步一步来的,所以我的代码是一样的。

examineJSONFile()方法中,我们有这行:

InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);

此文件直接从推特网站下载,如http://www.ibm.com/developerworks/xml/library/x-andbene1/#aotf第二段所述。

这一切都很棒,除了一件事:下载 Twitter 更新(推文)然后使用它作为原始文件构建应用程序绝对没有用。应该可以在运行时下载这个 JSON 文件,然后在 TextView 中显示推文。

我尝试以另一种方式创建InputStream,如下所示:

String url = "http://twitter.com/statuses/user_timeline/bbcnews.json";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuffer sb = new StringBuffer();
try
{
    String line = null;

    while ((line = br.readLine())!=null)
    {
        sb.append(line);
        sb.append('\n');                
    }
}
catch (IOException e)
{
        e.printStackTrace();
}
String jsontext = new String(sb.toString());

但似乎这一行:HttpResponse response = httpclient.execute(new HttpGet(url)); 引发异常。

有什么帮助吗?

【问题讨论】:

  • 如果您可以发布引发的异常的详细信息(至少第一行包含异常类型和消息字符串)会有所帮助

标签: java android json twitter inputstream


【解决方案1】:

您似乎缺少 INTERNET 权限。查看日志,您会清楚到底是什么问题。

【讨论】:

    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多