1

obj.optString("picture") 似乎没有将图片 url .jpeg 作为字符串。

public static List<Photo> getPictures(AuthProvider provider, String source, String type) {
List<Photo> photos = new LinkedList<Photo>();
if (provider.getProviderId() == Constants.FACEBOOK) {
    final String BASE_URL_FACEBOOK = "https://graph.facebook.com/";
    String url = BASE_URL_FACEBOOK + source + "/feed";

    try {
        Response response = provider.api(url, MethodType.GET.toString(), null, null, null);

        Log.d("AndroidAppPhotoUtil", "Status " + response.getStatus() + " returned by facebook get query " + url);
        if (response.getStatus() == 200) {
            String respStr = response.getResponseBodyAsString(Constants.ENCODING);
            JSONObject resp = new JSONObject(respStr);
            JSONArray data = resp.optJSONArray("data");
            if (data != null)
                for (int i = 0; i < data.length(); i++) {
                    JSONObject obj = data.getJSONObject(i);
                    Photo p = new Photo();

                    p.setId(obj.optString("id"));
                    p.setPicture(obj.optString("picture"));


                    if (isEmpty(type) || type.equals(obj.optString("type"))) {
                        photos.add(p);
                    }

                }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
return photos;  
}

.

"id": "466138393436802_466165356767439", 
      "from": {
    "name": "Dimitri Nicolopoulos", 
    "id": "100004358376630"
      }, 
      "to": {
    "data": [
      {
        "name": "My Test Event", 
        "start_time": "2012-11-13", 
        "location": "Saewrreritvh", 
        "id": "466138393436802"
      }
    ]
      }, 
      "picture": "http://photos-a.ak.fbcdn.net/hphotos-ak-ash3/533694_123231957832083_231056016_s.jpg", 
      "link": "http://www.facebook.com/photo.php?fbid=123231957832083&set=oa.160219667456793&type=1&relevant_count=1", 
      "icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif", 
4

1 回答 1

2

我添加了将您的 JSON 代码放在“{”和“}”中,然后我这样做了:

    String respStr = "{\"id\":\"466138393436802_466165356767439\",\"from\":{\"name\":\"Dimitri Nicolopoulos\",\"id\":\"100004358376630\"},\"to\":{\"data\":[{\"name\":\"My Test Event\",\"start_time\":\"2012-11-13\",\"location\":\"Saewrreritvh\",\"id\":\"466138393436802\"}]},\"picture\":\"http://photos-a.ak.fbcdn.net/hphotos-ak-ash3/533694_123231957832083_231056016_s.jpg\",\"link\":\"http://www.facebook.com/photo.php?fbid=123231957832083&set=oa.160219667456793&type=1&relevant_count=1\",\"icon\":\"http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif\"}";
    JSONObject resp = new JSONObject(respStr);
    System.out.println(resp.get("picture"));

这行得通。此外,您的数据始终为空。“数据”实际上是在“到”键下。所以你可以做

    String respStr = "{\"id\":\"466138393436802_466165356767439\",\"from\":{\"name\":\"Dimitri Nicolopoulos\",\"id\":\"100004358376630\"},\"to\":{\"data\":[{\"name\":\"My Test Event\",\"start_time\":\"2012-11-13\",\"location\":\"Saewrreritvh\",\"id\":\"466138393436802\"}]},\"picture\":\"http://photos-a.ak.fbcdn.net/hphotos-ak-ash3/533694_123231957832083_231056016_s.jpg\",\"link\":\"http://www.facebook.com/photo.php?fbid=123231957832083&set=oa.160219667456793&type=1&relevant_count=1\",\"icon\":\"http://static.ak.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif\"}";
    JSONObject resp = new JSONObject(respStr);
    JSONObject data = (JSONObject) resp.getJSONObject("to").getJSONArray("data").get(0);
    System.out.println(data);
于 2012-11-11T16:28:45.313 回答