2016-01-27 3 views
0

J'essaie d'analyser et de visualiser les données json. Les données ne sont pas affichées sur mon appareil. Normalement, je peux le faire plusieurs fois, mais dans ce cas je ne peux pas.Comment afficher la valeur Json Parse dans Android?

Voici mon code:

@Override 
    protected Void doInBackground(Void... params) { 
     // Create an array 
     arraylist = new ArrayList<HashMap<String, String>>(); 

     // Retrieve JSON Objects from the given URL address 


     jsonobject = JSONfunctions.getJSONfromURL("https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&order=viewCount&q=minecraft+mods&maxResults=50&key=<REDACTED>"); 

     try { 
      // Locate the array name in JSON 
      JSONArray jsonarray = jsonobject.getJSONArray("items"); 

      for (int i = 0; i < jsonarray.length(); i++) { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       jsonobject = jsonarray.getJSONObject(i); 
       // Retrive JSON Objects 
       JSONObject jsonObjId = jsonobject.getJSONObject("id"); 
       map.put("videoId", jsonObjId.getString("videoId")); 

       JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet"); 
       map.put("title", jsonObjSnippet.getString("title")); 

       //map.put("description", jsonObjSnippet.getString("description")); 
       // map.put("flag", jsonobject.getString("flag")); 

       JSONObject jsonObjThumbnail = jsonObjSnippet.getJSONObject("thumbnails"); 
       String imgURL = jsonObjThumbnail.getJSONObject("high").getString("url"); 
       map.put("url",imgURL); 


       // Set the JSON Objects into the array 
       arraylist.add(map); 
      } 
     } catch (JSONException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

Voici mon Json Respons

{ 
"items": [ 
    { 
    "kind": "youtube#searchResult", 
    "etag": "\"abQHWywil_AkNqdqji7_FqiK-u4/sYlQoR4h5RdFR24l2b2rLPG5lwA\"", 
    "id": { 
    "kind": "youtube#channel", 
    "channelId": "UCH-_hzb2ILSCo9ftVSnrCIQ" 
    }, 
    "snippet": { 
    "publishedAt": "2008-07-09T21:56:56.000Z", 
    "channelId": "UCH-_hzb2ILSCo9ftVSnrCIQ", 
    "title": "YOGSCAST Lewis & Simon", 
    "description": "Minecraft and multiplayer comedy gaming with a drunken dwarf and a handsome spaceman! Join us as we laugh our way through the best, the worst and the ...", 
    "thumbnails": { 
    "default": { 
     "url": "https://yt3.ggpht.com/-FMO2nSO2pP8/AAAAAAAAAAI/AAAAAAAAAAA/QZLWwqsqMIU/s512-c-k-no/photo.jpg" 
    }, 
    "medium": { 
     "url": "https://yt3.ggpht.com/-FMO2nSO2pP8/AAAAAAAAAAI/AAAAAAAAAAA/QZLWwqsqMIU/s512-c-k-no/photo.jpg" 
    }, 
    "high": { 
     "url": "https://yt3.ggpht.com/-FMO2nSO2pP8/AAAAAAAAAAI/AAAAAAAAAAA/QZLWwqsqMIU/s512-c-k-no/photo.jpg" 
    } 
    }, 
    "channelTitle": "BlueXephos", 
    "liveBroadcastContent": "none" 
    } 
    }] 
} 
+0

Vous devez supprimer votre clé API YouTube de l'exemple de code. –

+1

Que signifie 'échoué'? Avez-vous une exception? –

+0

c'est afficher juste une page blanche –

Répondre

0

Comme vous pouvez le voir dans l'image ci-dessous, il n'y a pas de clé "videoId" dans votre JSONObject jsonObjId.

Vous aurez une valeur null dans votre objet carte pour la clé "videoId". Probablement c'est pourquoi vos données ne sont pas montrées.

PS: Utilisez un modèle de classe au lieu de HashMap, il sera plus élégant.

+0

pouvez-vous me donner un exemple –

+0

Essayez de remplacer * jsonObjId.getString ("videoId") * par * jsonObjId.getString ("channelId") * – Rami