2016-04-11 74 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(); 
      } 
     } 
    }); 
} 

我使用http://dictionaryapi.net/作爲API。我的網址如下所示:「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." 
    ] 
}] 

這是一個數組填充一個對象。你應該可以得到這個數組,然後得到它的第一個元素。

0

如果我看着JSON在http://dictionaryapi.net/api/definition/awesome,它將不關鍵 「文檔」 返回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)趕上從您的網址jsonArray回報。

相關問題