Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用android图形API获取facebook用户的位置_Android_Facebook Graph Api_Facebook Sdk 4.0 - Fatal编程技术网

如何使用android图形API获取facebook用户的位置

如何使用android图形API获取facebook用户的位置,android,facebook-graph-api,facebook-sdk-4.0,Android,Facebook Graph Api,Facebook Sdk 4.0,我可以使用以下代码获取姓名、电子邮件、性别和出生日期 setReadPermissions(Arrays.asList(“public_profile,email,user_birth,user_friends,user_location”)//我还添加了用户位置 loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override

我可以使用以下代码获取姓名、电子邮件、性别和出生日期

setReadPermissions(Arrays.asList(“public_profile,email,user_birth,user_friends,user_location”)//我还添加了用户位置

    loginButton.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 object,GraphResponse response) {

                            Log.v("LoginActivity", response.toString());
                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender,birthday,location");   //and location parameter
            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.getCause().toString());
        }
    });

我怎么能得到这个

首先,您需要提供用户位置

然后在你的

public void onCompleted(JSONObject object,GraphResponse response){

    JSONObject jsonObj = response.getGraphObject().getInnerJSONObject();
    GraphUser user = GraphObject.Factory.create(jsonObj,GraphUser.class);
    if (user.getLocation() != null && getLocation().getProperty("name")!= null) {
        userObject.userCity = user.getLocation().getProperty("name").toString();
    }
}
这对我有用,希望对你有用


注意:您可能必须进行facebook评论才能获取用户位置。

如果不在facebook上提交应用程序进行评论,我们将无法获取facebook用户的位置。但出于测试目的,您可以获得应用程序开发人员、管理员或测试人员的位置

从这里下载源代码()

添加此依赖项:

compile 'com.facebook.android:facebook-android-sdk:4.0.1'
activity_main.xml

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/iv_image"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/profile"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="Name"
            android:gravity="center_vertical"
            android:textSize="15dp"
            android:textColor="#000000"

            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:text="Name"
            android:textSize="15dp"
            android:id="@+id/tv_name"
            android:gravity="center_vertical"
            android:textColor="#000000"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <TextView
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="Email"
        android:gravity="center_vertical"
        android:textSize="15dp"
        android:layout_below="@+id/tv_name"
        android:textColor="#000000"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/tv_name"
        android:text="Email"
        android:gravity="center_vertical"
        android:textSize="15dp"
        android:id="@+id/tv_email"
        android:textColor="#000000"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="DOB"
            android:gravity="center_vertical"
            android:textSize="15dp"
            android:textColor="#000000"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_below="@+id/tv_name"
            android:text="DOB"
            android:gravity="center_vertical"
            android:textSize="15dp"
            android:id="@+id/tv_dob"
            android:layout_toRightOf="@+id/tv_email"
            android:textColor="#000000"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:text="Location"
            android:gravity="center_vertical"
            android:textSize="15dp"
            android:textColor="#000000"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_below="@+id/tv_name"
            android:text="location"
            android:gravity="center_vertical"
            android:textSize="15dp"
            android:id="@+id/tv_location"
            android:textColor="#000000"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:background="#6585C8"
        android:id="@+id/ll_facebook"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="40dp"
        android:layout_height="50dp">

        <ImageView
            android:layout_width="50dp"
            android:src="@drawable/facebook"
            android:id="@+id/iv_facebook"
            android:layout_height="50dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login with Facebook"
            android:textSize="20dp"
            android:textColor="#FFFFFF"
            android:textStyle="bold"
            android:id="@+id/tv_facebook"
            android:layout_marginLeft="20dp"
            android:gravity="center"
            android:layout_gravity="center"

            />

    </LinearLayout>

    </LinearLayout>

MainActivity.java

package facebooklocation.facebooklocation;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import org.json.JSONObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    CallbackManager callbackManager;
    ImageView iv_image, iv_facebook;
    TextView tv_name, tv_email, tv_dob, tv_location, tv_facebook;
    LinearLayout ll_facebook;
    String str_facebookname, str_facebookemail, str_facebookid, str_birthday, str_location;
    boolean boolean_login;

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

        init();
        getKeyHash();
        listener();
    }


    private void init() {
        iv_image = (ImageView) findViewById(R.id.iv_image);
        iv_facebook = (ImageView) findViewById(R.id.iv_facebook);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_email = (TextView) findViewById(R.id.tv_email);
        tv_dob = (TextView) findViewById(R.id.tv_dob);
        tv_location = (TextView) findViewById(R.id.tv_location);
        tv_facebook = (TextView) findViewById(R.id.tv_facebook);
        ll_facebook = (LinearLayout) findViewById(R.id.ll_facebook);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
    }

    private void listener() {
        tv_facebook.setOnClickListener(this);
        ll_facebook.setOnClickListener(this);
        iv_facebook.setOnClickListener(this);

    }

    private void facebookLogin() {
        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.e("ONSUCCESS", "User ID: " + loginResult.getAccessToken().getUserId()
                        + "\n" + "Auth Token: " + loginResult.getAccessToken().getToken()
                );
                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {

                                    boolean_login = true;
                                    tv_facebook.setText("Logout from Facebook");

                                    Log.e("object", object.toString());
                                    str_facebookname = object.getString("name");

                                    try {
                                        str_facebookemail = object.getString("email");
                                    } catch (Exception e) {
                                        str_facebookemail = "";
                                        e.printStackTrace();
                                    }

                                    try {
                                        str_facebookid = object.getString("id");
                                    } catch (Exception e) {
                                        str_facebookid = "";
                                        e.printStackTrace();

                                    }


                                    try {
                                        str_birthday = object.getString("birthday");
                                    } catch (Exception e) {
                                        str_birthday = "";
                                        e.printStackTrace();
                                    }

                                    try {
                                        JSONObject jsonobject_location = object.getJSONObject("location");
                                        str_location = jsonobject_location.getString("name");

                                    } catch (Exception e) {
                                        str_location = "";
                                        e.printStackTrace();
                                    }

                                    fn_profilepic();

                                } catch (Exception e) {

                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, name, email,gender,birthday,location");

                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                if (AccessToken.getCurrentAccessToken() == null) {
                    return; // already logged out
                }
                new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
                        .Callback() {
                    @Override
                    public void onCompleted(GraphResponse graphResponse) {
                        LoginManager.getInstance().logOut();
                        LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile,email"));
                        facebookLogin();

                    }
                }).executeAsync();


            }

            @Override
            public void onError(FacebookException e) {
                Log.e("ON ERROR", "Login attempt failed.");


                AccessToken.setCurrentAccessToken(null);
                LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile,email,user_birthday"));
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        try {
            callbackManager.onActivityResult(requestCode, resultCode, data);
        } catch (Exception e) {

        }

    }

    private void getKeyHash() {
        // Add code to print out the key hash
        try {
            PackageInfo info = getPackageManager().getPackageInfo("facebooklocation.facebooklocation", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }
    }

    private void fn_profilepic() {

        Bundle params = new Bundle();
        params.putBoolean("redirect", false);
        params.putString("type", "large");
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "me/picture",
                params,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {

                        Log.e("Response 2", response + "");

                        try {
                            String str_facebookimage = (String) response.getJSONObject().getJSONObject("data").get("url");
                            Log.e("Picture", str_facebookimage);

                            Glide.with(MainActivity.this).load(str_facebookimage).skipMemoryCache(true).into(iv_image);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        tv_name.setText(str_facebookname);
                        tv_email.setText(str_facebookemail);
                        tv_dob.setText(str_birthday);
                        tv_location.setText(str_location);

                    }
                }
        ).executeAsync();
    }


    @Override
    public void onClick(View view) {

        if (boolean_login) {
            boolean_login = false;
            LoginManager.getInstance().logOut();
            tv_location.setText("");
            tv_dob.setText("");
            tv_email.setText("");
            tv_name.setText("");
            Glide.with(MainActivity.this).load(R.drawable.profile).into(iv_image);
            tv_facebook.setText("Login with Facebook");
        } else {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile,email,user_birthday,user_location"));
            facebookLogin();
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoginManager.getInstance().logOut();
    }
}
package facebooklocation.facebooklocation;
导入android.content.Intent;
导入android.content.pm.PackageInfo;
导入android.content.pm.PackageManager;
导入android.content.pm.Signature;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Base64;
导入android.util.Log;
导入android.view.view;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入com.bumptech.glide.glide;
导入com.facebook.AccessToken;
导入com.facebook.CallbackManager;
导入com.facebook.facebook回调;
导入com.facebook.facebook异常;
导入com.facebook.FacebookSdk;
导入com.facebook.GraphRequest;
导入com.facebook.GraphResponse;
导入com.facebook.HttpMethod;
导入com.facebook.login.LoginManager;
导入com.facebook.login.LoginResult;
导入org.json.JSONObject;
导入java.security.MessageDigest;
导入java.security.NoSuchAlgorithmException;
导入java.util.array;
公共类MainActivity扩展AppCompatActivity实现View.OnClickListener{
CallbackManager CallbackManager;
ImageView iv_图像,iv_facebook;
text查看电视名称、电视电子邮件、电视dob、电视位置、电视facebook;
线性布局的ll_facebook;
字符串str_facebook name、str_facebook email、str_facebook id、str_生日、str_位置;
布尔逻辑;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
getKeyHash();
监听器();
}
私有void init(){
iv_图像=(ImageView)findViewById(R.id.iv_图像);
iv_facebook=(ImageView)findViewById(R.id.iv_facebook);
tv_name=(TextView)findviewbyd(R.id.tv_name);
tv_email=(TextView)findviewbyd(R.id.tv_email);
tv_dob=(TextView)findviewbyd(R.id.tv_dob);
tv_位置=(TextView)findViewById(R.id.tv_位置);
tv_facebook=(TextView)findviewbyd(R.id.tv_facebook);
ll_facebook=(LinearLayout)findViewById(R.id.ll_facebook);
sdkinInitialize(this.getApplicationContext());
}
私有void侦听器(){
tv_facebook.setOnClickListener(这个);
ll_facebook.setOnClickListener(这个);
iv_facebook.setOnClickListener(本);
}
私有void facebookLogin(){
callbackManager=callbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback()){
@凌驾
成功时公共无效(LoginResult LoginResult){
Log.e(“ONSUCCESS”,“用户ID:+loginResult.getAccessToken().getUserId())
+“\n”+“身份验证令牌:”+loginResult.getAccessToken().getToken()
);
GraphRequest请求=GraphRequest.NewMereRequest(loginResult.getAccessToken(),
新建GraphRequest.GraphJSONObjectCallback(){
@凌驾
未完成公共无效(JSONObject对象,GraphResponse响应){
试一试{
布尔值=真;
tv_facebook.setText(“从facebook注销”);
Log.e(“object”,object.toString());
str_facebookname=object.getString(“name”);
试一试{
str_facebookemail=object.getString(“电子邮件”);
}捕获(例外e){
str_facebookemail=“”;
e、 printStackTrace();
}
试一试{
str_facebookid=object.getString(“id”);
}捕获(例外e){
str_facebook id=“”;
e、 printStackTrace();
}
试一试{
str_birth=object.getString(“生日”);
}捕获(例外e){
str_birth=“”;
e、 printStackTrace();
}
试一试{
JSONObject JSONObject_location=object.getJSONObject(“位置”);
str_location=jsonobject_location.getString(“名称”);
}捕获(例外e){
str_location=“”;
e、 printStackTrace();
}
package facebooklocation.facebooklocation;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import org.json.JSONObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    CallbackManager callbackManager;
    ImageView iv_image, iv_facebook;
    TextView tv_name, tv_email, tv_dob, tv_location, tv_facebook;
    LinearLayout ll_facebook;
    String str_facebookname, str_facebookemail, str_facebookid, str_birthday, str_location;
    boolean boolean_login;

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

        init();
        getKeyHash();
        listener();
    }


    private void init() {
        iv_image = (ImageView) findViewById(R.id.iv_image);
        iv_facebook = (ImageView) findViewById(R.id.iv_facebook);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_email = (TextView) findViewById(R.id.tv_email);
        tv_dob = (TextView) findViewById(R.id.tv_dob);
        tv_location = (TextView) findViewById(R.id.tv_location);
        tv_facebook = (TextView) findViewById(R.id.tv_facebook);
        ll_facebook = (LinearLayout) findViewById(R.id.ll_facebook);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
    }

    private void listener() {
        tv_facebook.setOnClickListener(this);
        ll_facebook.setOnClickListener(this);
        iv_facebook.setOnClickListener(this);

    }

    private void facebookLogin() {
        callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.e("ONSUCCESS", "User ID: " + loginResult.getAccessToken().getUserId()
                        + "\n" + "Auth Token: " + loginResult.getAccessToken().getToken()
                );
                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {

                                    boolean_login = true;
                                    tv_facebook.setText("Logout from Facebook");

                                    Log.e("object", object.toString());
                                    str_facebookname = object.getString("name");

                                    try {
                                        str_facebookemail = object.getString("email");
                                    } catch (Exception e) {
                                        str_facebookemail = "";
                                        e.printStackTrace();
                                    }

                                    try {
                                        str_facebookid = object.getString("id");
                                    } catch (Exception e) {
                                        str_facebookid = "";
                                        e.printStackTrace();

                                    }


                                    try {
                                        str_birthday = object.getString("birthday");
                                    } catch (Exception e) {
                                        str_birthday = "";
                                        e.printStackTrace();
                                    }

                                    try {
                                        JSONObject jsonobject_location = object.getJSONObject("location");
                                        str_location = jsonobject_location.getString("name");

                                    } catch (Exception e) {
                                        str_location = "";
                                        e.printStackTrace();
                                    }

                                    fn_profilepic();

                                } catch (Exception e) {

                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, name, email,gender,birthday,location");

                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                if (AccessToken.getCurrentAccessToken() == null) {
                    return; // already logged out
                }
                new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
                        .Callback() {
                    @Override
                    public void onCompleted(GraphResponse graphResponse) {
                        LoginManager.getInstance().logOut();
                        LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile,email"));
                        facebookLogin();

                    }
                }).executeAsync();


            }

            @Override
            public void onError(FacebookException e) {
                Log.e("ON ERROR", "Login attempt failed.");


                AccessToken.setCurrentAccessToken(null);
                LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile,email,user_birthday"));
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        try {
            callbackManager.onActivityResult(requestCode, resultCode, data);
        } catch (Exception e) {

        }

    }

    private void getKeyHash() {
        // Add code to print out the key hash
        try {
            PackageInfo info = getPackageManager().getPackageInfo("facebooklocation.facebooklocation", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }
    }

    private void fn_profilepic() {

        Bundle params = new Bundle();
        params.putBoolean("redirect", false);
        params.putString("type", "large");
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "me/picture",
                params,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {

                        Log.e("Response 2", response + "");

                        try {
                            String str_facebookimage = (String) response.getJSONObject().getJSONObject("data").get("url");
                            Log.e("Picture", str_facebookimage);

                            Glide.with(MainActivity.this).load(str_facebookimage).skipMemoryCache(true).into(iv_image);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        tv_name.setText(str_facebookname);
                        tv_email.setText(str_facebookemail);
                        tv_dob.setText(str_birthday);
                        tv_location.setText(str_location);

                    }
                }
        ).executeAsync();
    }


    @Override
    public void onClick(View view) {

        if (boolean_login) {
            boolean_login = false;
            LoginManager.getInstance().logOut();
            tv_location.setText("");
            tv_dob.setText("");
            tv_email.setText("");
            tv_name.setText("");
            Glide.with(MainActivity.this).load(R.drawable.profile).into(iv_image);
            tv_facebook.setText("Login with Facebook");
        } else {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile,email,user_birthday,user_location"));
            facebookLogin();
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoginManager.getInstance().logOut();
    }
}
   private void facebookLogin() {
        callbackManager=CallbackManager.Factory.create();
        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                getProfileDetail(loginResult.getAccessToken());
            }

            @Override
            public void onCancel() {
                Log.e("CANCEL : ","CANCEL");
                // App code

                if (AccessToken.getCurrentAccessToken() == null) {
                    return; // already logged out
                }
                new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
                        .Callback() {
                    @Override
                    public void onCompleted(GraphResponse graphResponse) {
                        LoginManager.getInstance().logOut();
                        LoginManager.getInstance().logInWithReadPermissions(FacebookActivity.this, Arrays.asList("public_profile,email"));
                        facebookLogin();

                    }
                }).executeAsync();
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                AccessToken.setCurrentAccessToken(null);
                LoginManager.getInstance().logInWithReadPermissions(FacebookActivity.this, Arrays.asList("public_profile,email,user_birthday"));
                Log.e("ERROR : ",exception.toString());
            }
        });
    }

    private void getProfileDetail(AccessToken accessToken) {
        GraphRequest graphRequest=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {

                String name = null;
                String email = null;
                String profileImage_link = null;
                String user_id = null;
                 String location = null;
                try {
                if (object.has("name"))
                {
                        name=object.getString("name");
                }

                if (object.has("email"))
                {
                    email=object.getString("email");
                }

                if (object.has("id"))
                {
                    user_id=object.getString("id");
                }

                profileImage_link=object.getJSONObject("picture").getJSONObject("data").getString("url");
                    Log.e("PICTURE URL : ",profileImage_link);

                    Log.e("Detail : ","Name : "+name+ "   Email : "+email+ "   User_id : "+user_id+"  Location : "+location);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.e("Object : ",object.toString());
                tv_response.setText(object.toString());
                String str_birthday =null;
                String str_location=null;
                try {
                    str_birthday = object.getString("birthday");
                } catch (Exception e) {
                    str_birthday = "";
                    e.printStackTrace();
                }

                try {
                    JSONObject jsonobject_location = object.getJSONObject("location");
                    str_location = jsonobject_location.getString("name");

                } catch (Exception e) {
                    str_location = "";
                    e.printStackTrace();
                }

                Log.e("LOCATION : ",str_location+"   "+str_birthday);
            }


        });

        Bundle bundle = new Bundle();
        bundle.putString(
                "fields",
                "id,name,link,email,gender,picture.type(large),last_name,first_name,locale,timezone,updated_time,verified,birthday,location"
        );
        graphRequest.setParameters(bundle);
        graphRequest.executeAsync();


    }