【发布时间】:2011-08-10 20:55:52
【问题描述】:
http://search.twitter.com/search.json?page=10&q=&geocode=37,-122,1mi&callback=myCallback
这是我的推特搜索网址。
如何获取响应对象?我一般都知道怎么解析一个JSON对象,是不是一样的?
谢谢!
【问题讨论】:
http://search.twitter.com/search.json?page=10&q=&geocode=37,-122,1mi&callback=myCallback
这是我的推特搜索网址。
如何获取响应对象?我一般都知道怎么解析一个JSON对象,是不是一样的?
谢谢!
【问题讨论】:
尝试使用 BufferedReader 和 StringBuilder(这是我目前在我的应用程序中使用的)。然后我使用字符串创建一个 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();
}
【讨论】: