facebook api得到图片的饲料

时间:2017-02-26 13:55:22

标签: android facebook facebook-graph-api

我正在使用Java从Facebook图形API获取用户的提要。 / me / feed工作正常但我想获得帖子的图片(如果有的话)。 在Graph api explorer上,以下URL用于检索图片:

/ ME /喂?字段=图像,消息,故事

但是当我改变我的代码时:

 final GraphRequest graphRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/feed", null, HttpMethod.GET, gCallback);

final GraphRequest graphRequest = new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/feed?fields=message,story", null, HttpMethod.GET, gCallback);

它开始与RuntimeException崩溃:

02-26 16:30:34.751: D/AndroidRuntime(28980): Shutting down VM
02-26 16:30:34.751: E/AndroidRuntime(28980): FATAL EXCEPTION: main
02-26 16:30:34.751: E/AndroidRuntime(28980): Process: com.aktuna.gear.facebook, PID: 28980
02-26 16:30:34.751: E/AndroidRuntime(28980): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.aktuna.gear.facebook/com.aktuna.gear.facebook.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.ActivityThread.deliverResults(ActivityThread.java:4925)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.ActivityThread.access$1600(ActivityThread.java:222)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.os.Looper.loop(Looper.java:158)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.ActivityThread.main(ActivityThread.java:7229)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at java.lang.reflect.Method.invoke(Native Method)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
02-26 16:30:34.751: E/AndroidRuntime(28980): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.aktuna.gear.facebook.MainActivity$1.onSuccess(MainActivity.java:71)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.aktuna.gear.facebook.MainActivity$1.onSuccess(MainActivity.java:1)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.facebook.login.LoginManager.finishLogin(LoginManager.java:508)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:192)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at com.aktuna.gear.facebook.MainActivity.onActivityResult(MainActivity.java:107)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.Activity.dispatchActivityResult(Activity.java:7137)
02-26 16:30:34.751: E/AndroidRuntime(28980):    at android.app.ActivityThread.deliverResults(ActivityThread.java:4921)
02-26 16:30:34.751: E/AndroidRuntime(28980):    ... 9 more

有什么不对? 如果我将字段添加到我的请求中,为什么图形请求会返回null?

感谢

1 个答案:

答案 0 :(得分:0)

感谢@CBroe我能够在SDK参考的示例中找到答案。

这是为了将来的场合:

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                   JSONObject object,
                   GraphResponse response) {
                // Application code
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();