2016-04-11 26 views
0

私のアプリケーションは私のonSuccessメソッドを実行できないようです。AsyncHttpClient JSON onSuccess関数が実行されていません

これは、ログに言う: "W/JsonHttpRH:するonSuccess(int型、ヘッダー[]、JSONArray)オーバーライドされなかったが、コールバックを受信しました"

は、多くの人が代わりにJSONObjectのJSONArrayを使用していますが、これは、私には当てはまりません。コードは次のとおりです。

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(); 
      } 
     } 
    }); 
} 

私はAPIとしてhttp://dictionaryapi.net/を使用しています。私のURLは次のようになります: "http://dictionaryapi.net/api/definition/awesome"これはJSONオブジェクトを返します。

答えて

0

私の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." 
    ] 
}] 

が1つのオブジェクトで満たされた配列です。あなたはおそらくこの配列を取得し、その配列の最初の要素を取得する必要があります。

0

私はhttp://dictionaryapi.net/api/definition/awesomeにJSONに見れば、それは重要な "ドキュメント" なしjsonArray(ないjsonObject)を返します:

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

はあなたのURLからjsonArrayリターンをキャッチするonSuccess(int statusCode, Header headers[], JSONArray success)を使用しています。

関連する問題