Android Facebook图形api,JSONException没有照片的价值

时间:2016-12-28 12:45:08

标签: android facebook facebook-graph-api

我正在尝试从Facebook图表api获取用户照片列表,但我一直收到错误:

listObjects

我搜索了所有其他可用的答案,并尝试了所有建议,但我无法找到导致错误的原因。

这是我用来拍照的功能:

JSONException No value for photos

我创建了一个测试用户并确保他们拥有正确的权限。它正在登录,我可以获得用户公开信息。

我认为这可能是一个权限问题所以我运行了这个:

public void getUserPhotos(){
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        try {
                            //This contains all the photos with array data>>link
                            JSONObject photosobject = object.getJSONObject("photos");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "photos{link}");
        request.setParameters(parameters);
        request.executeAsync();
    }

记录的响应是:

String permissions = AccessToken.getCurrentAccessToken().getPermissions().toString();
System.out.println(permissions);

所以这不是许可问题。

我登录了用户并添加了照片,因此不应该成为问题。

我看到一些示例使用[public_profile, user_friends, user_photos] 作为字段,其他人使用"photos{link}"

"images"给了我这个错误:

"photos{link}"

12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: org.json.JSONException: No value for photos 12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: at org.json.JSONObject.get(JSONObject.java:389) 12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: at org.json.JSONObject.getJSONObject(JSONObject.java:609) 12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: at com.mysampleapp.Fragments.ProfileFragment$1.onCompleted(ProfileFragment.java:294) 12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304) 12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: at com.facebook.GraphRequest$5.run(GraphRequest.java:1368) 12-28 22:34:23.991 9199-9199/com.amazon.mysampleapp W/System.err: at android.os.Handler.handleCallback(Handler.java:751) 12-28 22:34:23.992 9199-9199/com.amazon.mysampleapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 12-28 22:34:23.992 9199-9199/com.amazon.mysampleapp W/System.err: at android.os.Looper.loop(Looper.java:154) 12-28 22:34:23.992 9199-9199/com.amazon.mysampleapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077) 12-28 22:34:23.992 9199-9199/com.amazon.mysampleapp W/System.err: at java.lang.reflect.Method.invoke(Native Method) 12-28 22:34:23.992 9199-9199/com.amazon.mysampleapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 12-28 22:34:23.992 9199-9199/com.amazon.mysampleapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 字段只返回空指针异常。

我查看了grapi api文档,发现了这个:

  

/ me节点是一个转换为user_id的特殊端点   访问令牌所在的人(或Facebook页面的page_id)   目前正用于进行API调用。如果您有用户访问权限   令牌,你可以使用:

检索所有用户的照片      

获取graph.facebook.com / me / photos

这似乎是正确的,我正在运行我的请求,所以我不确定我做错了什么。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

mainActivity.java

   btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {

                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("Main", response.toString());
                            setProfileToView(object);
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();
        }


 public void showCustomDialog(JSONObject jsonObject)
{
     final Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.facebookdetails);
    dialog.setTitle("Facebook Details");
    email = (TextView)dialog.findViewById(R.id.email);
    facebookName = (TextView) dialog.findViewById(R.id.name);
    gender = (TextView) dialog.findViewById(R.id.gender);
    infoLayout = (LinearLayout) dialog.findViewById(R.id.layout_info);
    profilePictureView = (ProfilePictureView)dialog. findViewById(R.id.imageprofile);
    DateofBirth= (TextView)dialog. findViewById(R.id.dob);
    showProgressDialog();
    try {
        disconnectFromFacebook();
        if(jsonObject.has("birthday"))
        {
            DateofBirth.setText(jsonObject.getString("birthday"));

        }
        if(jsonObject.has("email")){
            email.setText(jsonObject.getString("email"));

        }
        if(jsonObject.has("gender")||jsonObject.has("name")) {
            gender.setText(jsonObject.getString("gender"));
            facebookName.setText(jsonObject.getString("name"));
        }
        profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
        profilePictureView.setProfileId(jsonObject.getString("id"));
        infoLayout.setVisibility(View.VISIBLE);
        mProgressDialog.dismiss();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    dialog.show();
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

}