AsyncHttpClient JSON onSuccess函数未执行

时间:2016-04-11 03:00:15

标签: java android json android-async-http

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

1 个答案:

答案 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返回。