我的应用程序似乎无法运行我的onSuccess方法。
它在日志中说:“W / JsonHttpRH:onSuccess(int,Header [],JSONArray)未被覆盖,但收到了回调”
很多人使用JSONArray而不是JSONObject,但事实并非如此。代码是:
private void fetchDictionary() {
client = new DictionaryClient();
client.getWords("awesome", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
JSONArray docs = null;
if(response != null) {
// Get the docs json array
docs = response.getJSONArray("docs");
// Parse json array into array of model objects
final ArrayList<Word> words = Word.fromJson(docs);
// Remove all words from the adapter
wordAdapter.clear();
// Load model objects into the adapter
for (Word word : words) {
wordAdapter.add(word); // add word through the adapter
}
wordAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
// Invalid JSON format, show appropriate error.
e.printStackTrace();
}
}
});
}
我正在使用http://dictionaryapi.net/作为API。我的URL看起来像:“http://dictionaryapi.net/api/definition/awesome”,它返回一个JSON对象。
答案 0 :(得分:0)
如果我在http://dictionaryapi.net/api/definition/awesome中查看json,它将返回jsonArray(而不是jsonObject)而不使用key&#34; docs&#34; :
[{
"Word": "awesome",
"PartOfSpeech": "adjective",
"Forms": [],
"Definitions": [
"Causing awe; appalling; awful; as, an awesome sight.",
"Expressive of awe or terror."
]
}]
使用onSuccess(int statusCode, Header headers[], JSONArray success)
从网址中捕获jsonArray返回。