2016-04-11 11 views
0

Uygulamam onSuccess yöntemimi çalıştırıyor gibi görünmüyor.AsyncHttpClient JSON onSuccess işlevi yürütülüyor

O günlüğüne şöyle diyor: "W/JsonHttpRH: onSuccess (int, Üstbilgi [], JSONArray) yeri etkilenmez, ancak geri arama alındığı"

Birçok insan yerine JSONObject nesnesi arasında JSONArray kullanın, ancak bu benimle durum böyle değil. Kod:

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

Bir API olarak http://dictionaryapi.net/ kullanıyorum. URL'miz şöyle görünür: "http://dictionaryapi.net/api/definition/awesome", bir JSON nesnesini döndürür. bir JSON nesnesi döndüren "http://dictionaryapi.net/api/definition/awesome" :

cevap

0

My URL benziyor.

Bu doğru değil. Bağlandığınız nesne, bir nesne ile dolu bir dizi olan

değerini döndürür. Muhtemelen bu diziyi almalı ve sonra ilk elemanını almalısın. i http://dictionaryapi.net/api/definition/awesome yılında json içine bakarsak

0

, bu anahtar "docs" olmadan jsonArray (değil JSONObject nesnesi) döndürür:

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

kullanımını onSuccess(int statusCode, Header headers[], JSONArray success) URL'nizden jsonArray getiriyi yakalamak.

İlgili konular