2016-12-28 66 views
0

我试图获得来自Facebook的图形API的用户的照片列表,但我不断收到此错误的值:Android的Facebook的图形API,JSONException的照片

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) 

"images"场只返回一个空指针异常。

我看着在grapi API文档,发现这个:

The /me node is a special endpoint that translates to the user_id of the person (or the page_id of the Facebook Page) whose access token is currently being used to make the API calls. If you had a user access token, you could retrieve all of a user's photos by using:

GET graph.facebook.com /me/photos

这似乎是正确的,我运行了我的要求,所以我不知道我做错了。

在此先感谢您的帮助。

+0

什么是返回的JSONObject的内容(尝试登录object.toString())? – dthulke

+0

嗨,对象回来了{“id”:“106121596557455”} –

+0

响应回来了{Response:responseCode:200,graphObject:{“id”:“106121596557455”},error:null} –

回答

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(); 
     } 
    }); 

}