0

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

This my url for a twitter search.

How do I get the response object? I know how to parse a JSON object in general, is it the same way?

thanks!

1 Answer 1

0

Try using a BufferedReader and StringBuilder (that's what I'm currently using for my application). I then use the String to create a JSONObject. Here's some template code you may use:

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();
}
2
  • Note that this uses the Simple JSON library under org.json
    – Vinay
    Aug 10, 2011 at 21:03
  • Thanks Vinay, I didnt know I was supposed to do that.
    – Archie
    Aug 11, 2011 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.