2016-11-17 2 views
0

ich versuche, diese Json von Facebook-Seite Fotos zu holen:Wie diese Json mit Volley in Android holen

https://graph.facebook.com/v2.8/233713473419953/photos/uploaded/?fields=images%2Ccreated_time%2Cname%2Calbum&access_token=1792039221044098%7CAUTDDgGHH8Tzfz_RTrIGnaOTer8

Mit Volley mit diesem Code:

private void fetchImages() { 

    pDialog.setMessage("Carregando Fotos..."); 
    pDialog.show(); 

    JsonArrayRequest req = new JsonArrayRequest(endpoint, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 
        pDialog.hide(); 

        images.clear(); 

        for (int i = 0; i < response.length(); i++) { 
         try { 
          JSONObject object = response.getJSONObject(i); 
          Image image = new Image(); 

          JSONObject album = response.getJSONObject(i); 
          image.setName(album.getString("name")); 

          JSONObject url = object.getJSONObject("images"); 
          image.setSmall(url.getString("source")); 
          image.setMedium(url.getString("source")); 
          image.setLarge(url.getString("source")); 
          image.setTimestamp(url.getString("created_time")); 

          images.add(image); 

         } catch (JSONException e) { 
          Log.e(TAG, "Json parsing error: " + e.getMessage()); 
          Toast.makeText(PhotosActivity.this, "Erro ao carregar as fotos!! Verifique sua conexão com a internet e tente novamente!", Toast.LENGTH_SHORT).show(); 
         } 
        } 


        mAdapter.notifyDataSetChanged(); 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e(TAG, "Error: " + error.getMessage()); 
      pDialog.hide(); 
     } 
    }); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(req); 
} 

Aber ich weiß nicht, warum das nicht funktioniert, Schau mein Fehlerprotokoll:

E/PhotosActivity: Error: org.json.JSONException: Value {"data":[{"images":[{"height":1080,"source":"https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/15025513_559017034222927_9118471543496511523_o.jpg","width":1080},{"height":960,"source":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/15134703_559017034222927_9118471543496511523_n.jpg?oh=13ff804fb169acfd7ce920b838307094&oe=58BEEE0C","width":960},{"height":720,"source":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/p720x720\/15134703_559017034222927_9118471543496511523_n.jpg?oh=8b464f6f0cea6c0cf5f43143a2a52d6f&oe=58CB047D","width":720},{"height":600,"source":"https:\/\/fb-s-a-a.akamaihd.net\/h-ak-xft1\/v\/t1.0-0\/p600x600\/15134703_559017034222927_9118471543496511523_n.jpg?oh=a0cf471c3983fa1ed6be76e80ef5b4b1&oe=58BD1257&__gda__=1490108287_654543542cb382e20b8ccf22c53fc940","width":600},{"height":480,"source":"https:\/\/fb-s-a-a.akamaihd.net\/h-ak-xft1\/v\/t1.0-0\/p480x480\/15134703_559017034222927_9118471543496511523_n.jpg?oh=dd7df79fe92bb542b91f65c2a78edaad&oe=58C393B7&__gda__=1489225631_ae3d0517c84a89b98c70a2ade8d454f3","width":480},{"height":320,"source":"https:\/\/fb-s-a-a.akamaihd.net\/h-ak-xft1\/v\/t1.0-0\/p320x320\/15134703_559017034222927_9118471543496511523_n.jpg?oh=f65125e4e9098e36d494a5ca0596cdc6&oe=588B1EED&__gda__=1489373637_8c9ed6992b75d119dce4dbb82d1b3fee","width":320},{"height":540,"source":"https:\/\/fb-s-a-a.akamaihd.net\/h-ak-xft1\/v\/t1.0-0\/p180x540\/15134703_559017034222927_9118471543496511523_n.jpg?oh=a605e018bb94850ccb4f61721387f09e&oe=58D3DC50&__gda__=1490253944_5d04db9710cf69f5f8a34bd1d8f1ecb5","width":540},{"height":130,"source":"https:\/\/fb-s-a-a.akamaihd.net\/h-ak-xft1\/v\/t1.0-0\/p130x130\/15134703_559017034222927_9118471543496511523_n.jpg?oh=3d31ffdccabd8eb2cef549841e03983b&oe=58D57382&__gda__=1490237354_5ac451bf19bee0665e43ba4bc35bd0f1","width":130},{"height":225,"source":"https:\/\/fb-s-a-a.akamaihd.net\/h-ak-xft1\/v\/t1.0-0\/p75x225\/15134703_559017034222927_9118471543496511523_n.jpg?oh=279e3dd5f02d74f3b92b97209e1c511f&oe=588835F6&__gda__=1488842348_baff2cb6c9d6661503a346c279bf30eb","width":225}],"created_time":"2016-11-16T18:50:09+0000","name":"#Quinta é dia da mulherada se encontrar no #LugarDeNovosComeços a partir das 15h para crescer em Deus, para compartilhar, para fazer novas amizades!!! Então não fica de fora! Convida suas amigas e vem pra cá!\n.\n.\n.\n#LagoinhaNiteroi #MulheresLagoinhaNiteroi #VemPraCa #VemPraJesus #Experimenta","album":{"created_time":"2013-06-04T22:08:15+0000","name":"Timeline Photos","id":"233720070085960"},"id":"559017034222927"},{"images":[{"height":1365,"source":"https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/15137528_558559867601977_1587910935337058547_o.jpg","width":2048},{"height":960,"source":"https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/p960x960\/15137528_558559867601977_1587910935337058547_o.jpg","width":1440},{"height":720,"source":"https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/p720x720\/15137528_558559867601977_1587910935337058547_o.jpg","width":1080},{"height":600,"source":"https:\/\/fbcdn-photos-a-a.akamaihd.net\/hphotos-ak-xtf1\/t31.0-0\/p600x600\/15137528_558559867601977_1587910935337058547_o.jpg","width":900},{"height":480,"source":"https:\/\/fbcdn-photos-a-a.akamaihd.net\/hphotos-ak-xtf1\/t31.0-0\/p480x480\/15137528_558559867601977_1587910935337058547_o.jpg","width":720},{"height":320,"source":"https:\/\/fbcdn-photos-a-a.akamaihd.net\/hphotos-ak-xtf1\/v\/t1.0-0\/q83\/p320x320\/15095087_558559867601977_1587910935337058547_n.jpg?oh=623efbdcfe8c0aaa8eb5b74b96899109&oe=58C0951B&__gda__=1488708150_691d164f5ade13aead6e70c49a89b1de","width":480},{"height":540,"source":"https:\/\/fbcdn-photos-a-a.akamaihd.net\/hphotos-ak-xtf1\/t31.0-0\/q83\/p180x540\/15137528_558559867601977_1587910935337058547_o.jpg","width":810},{"height":130,"source":"https:\/\/fbcdn-photos-a-a.akamaihd.net\/hphotos-ak-xtf1\/v\/t1.0-0\/q83\/p130x130\/15095087_558559867601977_1587910935337058547_n.jpg?oh=0b0254ba932abd5f5e1cd555d237792b&oe=5888D974&__gda__=1490327641_797338b634f0523de1bfef4bf34bfd8d","width":195},{"height":2 

Was ich will ist, holen Sie nur den Namen des Albums, Quelle URL jedes Bildes und created_time.

Kann mir jemand mit diesem Problem helfen? Vielen Dank! ;)

Antwort

0

Das sollte zurück Form JSONArray

public void onResponse(JSONArray response) 

werden, während die zurückgegebenen Daten JSON-Objekt ist (wie ich in den Fehler sehen Sie gegeben haben).

Sie können StringRequest für Ihre Zwecke verwenden, wie:

StringRequest stringRequest = new StringRequest(endpoint, 
         new Response.Listener<String>() { 
          @Override 
          public void onResponse(String response) { 

           Log.d(TAG, response.toString()); 
           pDialog.hide(); 

           images.clear(); 

     Error-Line----->  JSONObject responseObj = new JSONObject(response);    //Here you have to cast string response as JSON Object 

           JSONArray returnedData = responseObj.getJSONArray("data"); 

           for (int i = 0; i < returnedData.length(); i++) { 
            try { 


             JSONObject object = returnedData.getJSONObject(i); 
             Image image = new Image(); 

             JSONObject album = response.getJSONObject(i); 
             image.setName(album.getString("name")); 

             JSONObject url = object.getJSONObject("images"); 
             image.setSmall(url.getString("source")); 
             image.setMedium(url.getString("source")); 
             image.setLarge(url.getString("source")); 
             image.setTimestamp(url.getString("created_time")); 

             images.add(image); 

            } catch (JSONException e) { 
             Log.e(TAG, "Json parsing error: " + e.getMessage()); 
             Toast.makeText(PhotosActivity.this, "Erro ao carregar as fotos!! Verifique sua conexão com a internet e tente novamente!", Toast.LENGTH_SHORT).show(); 
            } 
           } 


           mAdapter.notifyDataSetChanged(); 

          } 
         }, new Response.ErrorListener() { 
          @Override 
          public void onErrorResponse(VolleyError error) { 

           Log.e(TAG, "Error: " + error.getMessage()); 
           pDialog.hide(); 

         } 
        }); 


       RequestQueue requestQueue = Volley.newRequestQueue(this); 
       stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
       requestQueue.add(stringRequest); 

Hoffe, es hilft!