2016-12-28 32 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(); 
     } 
    }); 

}