开发者

Reading JSON data from the net (Twitter)

I found this great tutorial on how to use JSON to retrieve Twitter updates, and post it in a TextView: http://www.ibm.com/developerworks/xml/library/x-andbene1/

I've followed this tutorial step by step, so my code is the same.

In the method examineJSONFile(), we have this line:

InputStream is = this.getResources().openRawResource(R.r开发者_JAVA技巧aw.jsontwitter);

This file is downloaded directly from the Twitter website, as mentioned in the second paragraph of http://www.ibm.com/developerworks/xml/library/x-andbene1/#aotf.

All this is great, except for one thing: it's absolutely no use that one has to download the Twitter updates (tweets) and then build the app using this as a raw file. It should be possible to download this JSON file at runtime, and then show the tweets in the TextView afterwards.

I have tried to create the InputStream in another way, like this:

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

But it seems this line: HttpResponse response = httpclient.execute(new HttpGet(url)); throws an exception.

Any help please?


You seem to be missing the INTERNET permission. Look at the logs and it would be clear what exactly is the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜