2016-04-01 11 views
0

Facebook ile bağlantı kurmak ve kullanıcı adı, e-posta kimliği ve Profil bağlantısını ayıklamak için kod yazdım. Ayıklanan ayrıntılar, bir düğmenin tıklatıldığında farklı etkinliklerde görüntülenir. Ancak Ben onSuccess sonra kodu düzenlemeniz gerekir amaBir metin görünümünde Facebook'tan ayıklanan veriler nasıl görüntülenir

public class MainActivity extends AppCompatActivity { 
CallbackManager callbackManager; 
Button share,details; 
ShareDialog shareDialog; 
LoginButton login; 
ProfilePictureView profile; 
Dialog details_dialog; 
TextView details_txt; 
TextView details_txtx; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    setContentView(R.layout.activity_main); 
    callbackManager = CallbackManager.Factory.create(); 

    login = (LoginButton)findViewById(R.id.login_button); 
    profile = (ProfilePictureView)findViewById(R.id.picture); 
    shareDialog = new ShareDialog(this); 
    share = (Button)findViewById(R.id.share); 

    details = (Button)findViewById(R.id.details); 
    login.setReadPermissions("public_profile email"); 
    share.setVisibility(View.INVISIBLE); 


    details.setVisibility(View.INVISIBLE); 
    details_dialog = new Dialog(this); 
    details_dialog.setContentView(R.layout.dialog_details); 
    details_dialog.setTitle("Details"); 
    details_txtx = (TextView)details_txtx.findViewById(R.id.details_tetx); 

    details.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      details_dialog.show(); 
     } 
    }); 

    if(AccessToken.getCurrentAccessToken() != null){ 
     RequestData(); 
     share.setVisibility(View.VISIBLE); 
     details.setVisibility(View.VISIBLE); 
    } 
    login.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(AccessToken.getCurrentAccessToken() != null) { 
       share.setVisibility(View.INVISIBLE); 
       details.setVisibility(View.INVISIBLE); 
       profile.setProfileId(null); 
      } 
     } 
    }); 
    share.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      ShareLinkContent content = new ShareLinkContent.Builder().build(); 
      shareDialog.show(content); 

     } 
    }); 
    login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 


      if(AccessToken.getCurrentAccessToken() != null){ 
       RequestData(); 
       share.setVisibility(View.VISIBLE); 
       details.setVisibility(View.VISIBLE); 
      } 
     } 

     @Override 
     public void onCancel() { 

     } 

     @Override 
     public void onError(FacebookException exception) { 
     } 
    }); 

} 
public void RequestData(){ 
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
     @Override 
     public void onCompleted(JSONObject object,GraphResponse response) { 

      JSONObject json = response.getJSONObject(); 
      try { 
       if(json != null){ 
        String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link"); 
        details_txt.setText(Html.fromHtml(text)); 
        profile.setProfileId(json.getString("id")); 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    Bundle parameters = new Bundle(); 
    parameters.putString("fields", "id,name,link,email,picture"); 
    request.setParameters(parameters); 
    request.executeAsync(); 
} 

temizle olmak kodunu nasıl yok yok demektir successfully.which en kısa sürede giriş işlemi tamamlandıktan aynı actiivty görüntülemek i istediği Başarılı bir giriş yaptıktan sonra aynı aktivitede ayıklanan detayları görüntülemek için lütfen yardım edin.

cevap

0

aşağıdaki kodu

protected void connectToFacebook() { 
    ArrayList<String> list = new ArrayList<String>(); 
    list.add("email"); 
    // LoginManager.getInstance().logInWithReadPermissions(this, list); 
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile")); 

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 
      // App code 
      GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
       @Override 
       public void onCompleted(JSONObject json, GraphResponse response) { 
        // Application code 
        if (response.getError() != null) { 
         System.out.println("ERROR"); 
        } else { 
         System.out.println("Success"); 
         String jsonresult = String.valueOf(json); 
         System.out.println("JSON Result" + jsonresult); 

         String fbUserId = json.optString("id"); 
         String fbUserFirstName = json.optString("name"); 
         String fbUserEmail = json.optString("email"); 
         String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large"; 
         callApiForCheckSocialLogin(fbUserId, fbUserFirstName, fbUserEmail, fbUserProfilePics, "fb"); 
        } 
        Log.v("FaceBook Response :", response.toString()); 
       } 
      }); 
      Bundle parameters = new Bundle(); 
      parameters.putString("fields", "id,name,email,gender, birthday"); 
      request.setParameters(parameters); 
      request.executeAsync(); 


     } 

     @Override 
     public void onCancel() { 
      // App code 
      Log.v("LoginActivity", "cancel"); 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      // App code 
      // Log.v("LoginActivity", "" + exception); 
      Utilities.showToast(mActivity, "" + exception); 
     } 
    }); 
} 
kontrol ediniz
İlgili konular