facebook login android

3

I want to obtain the data of the public profile {email, id and the profile picture}, but when I start my application it falls, then I will leave my code

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        if(AccessToken.getCurrentAccessToken()==null){
            String data = getIntent().getExtras().getString("email");
            email=data;
            NavigationView navigationsView = (NavigationView) findViewById(R.id.nav_view);
            View hView =  navigationsView.getHeaderView(0);
            TextView nav_user = (TextView)hView.findViewById(R.id.txtMail);
            nav_user.setText(email);
        }
        else{
           GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {
                    try {
                        user.setEmail(response.getJSONObject().get("email").toString());
                        user.setName(response.getJSONObject().get("name").toString());
                        user.setImageUri(response.getJSONObject().get("picture").toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,email");
            request.setParameters(parameters);
            request.executeAsync();

            String correos=user.getEmail();

            NavigationView navigationsView = (NavigationView) findViewById(R.id.nav_view);
            View hView =  navigationsView.getHeaderView(0);
            TextView nav_user = (TextView)hView.findViewById(R.id.txtMail);
            nav_user.setText(correos);

            ImageView fperfil=(ImageView) hView.findViewById(R.id.imageView);
            fperfil.setImageResource(Integer.parseInt(user.getImageUri()));

        }

and this is my logcat

11-03 16:15:11.755 30076-30076/com.ciclomapp.ciclomapp.ciclomapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.ciclomapp.ciclomapp.ciclomapp, PID: 30076
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ciclomapp.ciclomapp.ciclomapp/com.ciclomapp.ciclomapp.ciclomapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.facebook.AccessToken com.facebook.GraphRequest.getAccessToken()' on a null object reference
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:156)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:211)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5371)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.facebook.AccessToken com.facebook.GraphRequest.getAccessToken()' on a null object reference
                                                                                       at com.ciclomapp.ciclomapp.ciclomapp.MainActivity.onCreate(MainActivity.java:103)
                                                                                       at android.app.Activity.performCreate(Activity.java:5990)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:156) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                       at android.os.Looper.loop(Looper.java:211) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5371) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740) 
    
asked by zhet 03.11.2016 в 19:16
source

1 answer

0

You are not considering several steps like for example the existence of the keys .. and your "picture" key does not exist in FB 4.5

This should help you to get the profile photo, first and last name

private void obtenerDatos( JSONObject object) {
    String id_fb = "";
    String foto = "";
    String nombre = "";
    String apellido = "";
    String email = "";
    try {
        id_fb = object.getString("id");

        try {
            URL profile_pic = new URL("https://graph.facebook.com/" + id_fb + "/picture?width=200&height=150");
            Log.i("profile_pic", profile_pic + "");
            foto = profile_pic.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        if (object.has("first_name"))
            nombre = object.getString("first_name");
        if (object.has("last_name"))
            apellido = object.getString("last_name");
        if (object.has("email"))
            email = object.getString("email");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.d("FOTO", foto);
    Log.d("NOMBRE", nombre);
    Log.d("APELLIDO", apellido);
    Log.d("EMAIL", email);

    //GUARDAR CON PREFENCIA
    SharedPreferences preferencia = getSharedPreferences("preferencia_fb", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferencia.edit();
    editor.putString("foto_fb", foto);
    editor.putString("nombre_fb", nombre);
    editor.putString("apellido_fb", apellido);
    editor.putString("email_fb", email);
    editor.commit(); //COMMIT

    //en este caso ya tengo una instancia de preferencia y siempre recuerda que las preferencias crean pequeños archivos en los móviles
    //haciendo que "preferencia_fb" sea el nombre de mi archivo debes ingresar siempre "preferencia_fb" para acceceder a sus variables que tenga
    String foto_guardada = preferencia.getString("foto_fb", "no se recibió nada"); //ASÍ LLAMAS UNA VARIABLE DE PREFERENCIA, EL SEGUNDO PARÁMETRO ES PARA VALIDAD NULLIDAD
}
    
answered by 03.11.2016 / 19:43
source