我正在尝试执行此JSON调用,如下所示:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=insouciant&sl=en&tl=en");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try{
responseBody = client.execute(get, responseHandler);
}catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
JSONParser parser=new JSONParser();
try {
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
System.out.println("JSONRESPONSE getCardsFromTagNames="+jsonObject);
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
}
});
不幸的是,返回的结果是NULL。我究竟做错了什么?谢谢!
答案 0 :(得分:2)
resultBody
包含回调函数,该函数通常由某些JavaScript(JSONP)调用。
由于您不需要此功能,您可以轻松地将其剥离:
HttpGet get = new HttpGet("http://www.google.com/dictionary/json?callback=a&q=insouciant&sl=en&tl=en");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try{
responseBody = client.execute(get, responseHandler);
responseBody = responseBody.substring(2, responseBody.length()-3);
}catch(Exception ex) {
ex.printStackTrace();
}
答案 1 :(得分:1)
您的结果将包含在函数中,以便在回调中使用(dict_api.callbacks.id100
)。如果省略回调,这个API似乎没有返回任何内容,你必须获取结果字符串,并解析出json部分,然后将其传递给你的json解析器。