如何解析Twitter搜索响应中的JSON

时间:2011-08-10 20:55:53

标签: android twitter

http://search.twitter.com/search.json?page=10&q=&geocode=37,-122,1mi&callback=myCallback

这是我的Twitter搜索网址。

如何获取响应对象?我知道如何解析一般的JSON对象,是否也是这样?

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用BufferedReader和StringBuilder(这就是我目前正在使用的应用程序)。然后我使用String来创建一个JSONObject。以下是您可能使用的一些模板代码:

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(//YOUR URL STRING HERE);
    HttpResponse response;
    response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    InputStream in = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder sb = new StringBuilder();

    String input = null;
    try {
        while ((input = reader.readLine()) != null) {
                    sb.append(input + "\n");
        }
    } catch (IOException e) {
            e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    String enter = sb.toString();
        JSONObject obj = new JSONObject(enter);

                //DO WHATEVER YOU WANT WITH THE JSONObject here

        in.close();
} catch(MalformedURLException e){
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (JSONException e){
    e.printStackTrace();
}