AsyncHttpClient JSON onSuccess函数未执行



我的应用程序似乎无法运行我的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对象。

我的URL看起来像:"http://dictionaryapi.net/api/definition/awesome"返回一个JSON对象。

这不是真的。您链接的内容返回

[{
  "Word": "awesome",
  "PartOfSpeech": "adjective",
  "Forms": [],
  "Definitions": [
    "Causing awe; appalling; awful; as, an awesome sight.",
    "Expressive of awe or terror."
  ]
}]

其是填充有一个对象的数组。你可能应该得到这个数组,然后得到它的第一个元素。

如果我在http://dictionaryapi.net/api/definition/awesome,它将返回没有密钥"docs"的jsonArray(而不是jsonObject):

[{
  "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)捕获URL返回的jsonArray。

最新更新