从Facebook检索数据到Android

时间:2018-04-24 02:59:26

标签: java android facebook android-studio

目前我正在使用一个Android项目,用户应该可以使用按钮登录到Facebook - 与Facebook连接,只需点击一下即可重定向到下一个活动,其中包含所有收集的数据。我不知道我在哪里弄错了。在此先感谢,代码如下。

测试类是Facebook SDK初始化类,结果类是帐户

测试类

public class Test extends AppCompatActivity {
    private static final String TAG = "SocialActivity";
    TextView txt_login_Status;
    LoginButton loginButton;
    CallbackManager callbackManager;
    ProgressDialog progressDialog;
    URL profile_pic;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        FacebookSdk.sdkInitialize(getApplicationContext());
        loginButton = (LoginButton)findViewById(R.id.login_button);
        callbackManager = CallbackManager.Factory.create();
        //loginButton.setReadPermissions("email");
        loginButton.setReadPermissions("public_profile");

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(final LoginResult loginResult) {
                progressDialog = new ProgressDialog(Test.this);
                progressDialog.setMessage("Please Wait! Account's being processed!");
                progressDialog.show();
                String accessToken = loginResult.getAccessToken().getToken();
                Log.i("accessToken",accessToken);
                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.i("Test",response.toString());
                            progressDialog.hide();
                            Toast.makeText(Test.this,"Success!! Logged into Account as per given user information. ",Toast.LENGTH_LONG).show();

                            Bundle bFacebookData = getFacebookData(object);
                            String firstname = bFacebookData.getString( "first_name");
                            String lastname = bFacebookData.getString( "last_name");
                            String email = bFacebookData.getString( "email");
                            String profilepic = bFacebookData.getString( "profile_pic");

                            Intent in = new Intent(Test.this, Account.class);
                            in.putExtra("firstname", firstname);
                            in.putExtra("lastname", lastname);
                            in.putExtra("email", email);
                            in.putExtra("Image", profilepic);
                            startActivity(in);

                    }
                });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, first_name, last_name, email");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
            }

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

    private Bundle getFacebookData(JSONObject object) {

        Bundle bundle = new Bundle();
        try{
            id = object.getString("id");
            profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=100&height=100");
            Log.i("profile_pic", profile_pic +"");
            bundle.putString("profile_pic",profile_pic.toString());
        }catch (JSONException ex){
            ex.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        bundle.putString("idFacebook",id);

        if(object.has("first_name")){
            try{
                bundle.putString("first_name", object.getString("first_name"));
            }catch (JSONException ex){
                ex.printStackTrace();
            }
        }

        if(object.has("last_name")){
            try{
                bundle.putString("last_name", object.getString("last_name"));
            }catch (JSONException ex){
                ex.printStackTrace();
            }
        }

        if(object.has("email")){
            try{
                bundle.putString("email", object.getString("email"));
            }catch (JSONException ex){
                ex.printStackTrace();
            }
        }
        return bundle;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

帐户类

public class Account extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    FirebaseAuth firebaseAuth;
    Button btnSignOut;
    TextView textName, textEmail;
    FirebaseUser firebaseUser;
    ProgressDialog progressDialog;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        btnSignOut = (Button) findViewById(R.id.btnSignOut);
        textName = (TextView) findViewById(R.id.textName);
        textEmail = (TextView) findViewById(R.id.textEmail);
        imageView = (ImageView) findViewById(R.id.profileImage);

        String firstname = getIntent().getExtras().getString("first_name");
        String lastname = getIntent().getExtras().getString("last_name");
        String email = getIntent().getExtras().getString("email");
        String profilepic =getIntent().getExtras().getString("profile_pic");

        if (first_name != null && last_name != null && email != null && profile_pic != null) {
                textName.setText(first_name +" "+last_name);
                textEmail.setText(email);
                Glide.with(Account.this).load(profile_pic).into(imageView);
        } else {
            Toast.makeText(this.getApplicationContext(), "Data isnt Found!", Toast.LENGTH_LONG).show();
        }
        progressDialog = new ProgressDialog(this);


    }
}

我一直在关注YouTube教程,链接https://www.youtube.com/watch?v=R50t9pHtWM0 Facebook Tutorial以45分钟的视频用外语开始。

我在我的模拟器上收到的消息是来自帐户活动的Toast消息:Toast.makeText(this.getApplicationContext(),&#34; Data isnt Found!&#34;,Toast.LENGTH_LONG)。show();

Facebook登录工作正常,问题是数据无法获取。

2 个答案:

答案 0 :(得分:0)

我希望这对你有用

insted的

# Sample data
data <- cbind.data.frame(Publisher = c("facebook", "google display", "other"));

data$the_value_i_want <- keyval$val[match(data$Publisher, keyval$key)];
data;
#       Publisher the_value_i_want
#1       facebook                6
#2 google display                3
#3          other               NA

使用帐户类

 String firstname = getIntent().getExtras().getString("first_name");
 String lastname = getIntent().getExtras().getString("last_name");
 String email = getIntent().getExtras().getString("email");
 String profilepic =getIntent().getExtras().getString("profile_pic");

答案 1 :(得分:0)

问题在这里,我真的不知道为什么。

但是在帐户类中,我给了这个

if (first_name != null) {

而不是这个

if (first_name != null && last_name != null && email != null && profile_pic != null) {