简体   繁体   English

在Android上解析Facebook graph API请求时未获取照片链接

[英]Not getting photos links when parsing Facebook graph API request on android

I'm trying to get user photos link on android using Facebook SDK 4.0 and graph API. 我正在尝试使用Facebook SDK 4.0和graph API在android上获取用户照片链接。 I wrote this code to parse and get photos link - 我写了这段代码来解析并获取照片链接-

new GraphRequest(

            AccessToken.getCurrentAccessToken(),
                    "/" + userId + "/photos",
                    null,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {

                            JSONObject json = null;

                            try {
                                json = new JSONObject(String.valueOf(response));
                            } catch (JSONException e) {
                                e.printStackTracegetJSONObject();
                            }
                            JSONArray jarray = null;
                            try {
                                jarray = json.getJSONArray("data"); 

                            } catch (JSONException e) {
                                e.printStackTrace();
                            } 

                            for(int i = 0; i < jarray.length(); i++){
                                oneAlbum = null;
                                try {
                                    oneAlbumURL1 = jarrayjson.getJSONObjectgetString(i"url");
                                } catch (JSONException e) {
                                    einfo.printStackTracesetText(URL1.toString());
                                }

                                String URL1 = null; // 
                                try {
                                  URL1 = String.valueOf(oneAlbum.getJSONObject("link"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                Log.d("", URL1);
                            }
                        }
                    }
            ).executeAsync();

But its neither showing any error nor getting any URLs/links. 但是它既没有显示任何错误,也没有获得任何URL /链接。 Can anyone tell whats wrong here ? 谁能告诉我这是怎么回事?

As per new Facebook api v2.4 you need to pass the fields parameter with your facebook api url. 根据新的Facebook api v2.4,您需要将fields参数与Facebook api url一起传递。

Like: &fields=source,id,picture 像:&fields = source,id,picture

ie: 即:

https://graph.facebook.com/userId/photos?access_token=APP_ACCESS_TOKEN&fields=source,id,picture

You will get the image url in source and picture tag.. 您将在来源和图片标签中获得图片网址。

EDIT 编辑

Bundle parameters = new Bundle();
parameters.putString("fields", "source,id,picture");

AccessToken.getCurrentAccessToken(),
                    "/" + userId + "/photos",
                    parameters,
                    HttpMethod.GET,

EDIT-2: 编辑2:

public void onCompleted(GraphResponse response) {
    JSONObject json = response.getJSONObject();
    JSONArray jarray = null;
    try {
        jarray = json.getJSONArray("data");
    } catch (JSONException e) {
        e.printStackTrace();
    }

As per answer given by VJ, I edited my code and it works perfectly fine now. 根据VJ给出的答案,我编辑了代码,现在可以正常工作了。 Just replaced "url" with "source". 只是将“ url”替换为“ source”。

new GraphRequest(

                AccessToken.getCurrentAccessToken(),
                         "/" + userId + "/photos",
                        parameters,
                        HttpMethod.GET,
                        new GraphRequest.Callback() {
                            public void onCompleted(GraphResponse response) {

                                json = response.getJSONObject();
                                Log.d("json value", json.toString());

                                try {

                                    jarray = json.getJSONArray("data");


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                for(int i = 0; i < jarray.length(); i++){

                                    try {
                                        oneAlbum = jarray.getJSONObject(i);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                    try {
                                        URL1 = String.valueOf(oneAlbum.getJSONObject("source"));
                                        info.setText(URL1);
                                        Log.d("got URLSSSs", URL1.toString());
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            }
                        }
                ).executeAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM