Android LoginActivity, Facebook CallbackManager with Graph API: executeAndWait () method not working

I am new to android and I am creating an application that requires login. The google + and basic email accounts worked so far, but I'm having trouble logging into Facebook. Most of the code I have is sourced from the developer documentation on Facebook and I have tried to use the problem in different ways without any success.

This is my onCreate method where I set up the callbackManager and loginButton. LoginButton registers callbacks. If it is successful, it will start requesting the schedule. However, the method executeAndWait()

never works.

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_login);

    mFacebookCallbackManager = CallbackManager.Factory.create();

    // Set up facebook login button
    LoginButton facebookLoginButton = (LoginButton) findViewById(R.id.facebook_sign_in_button);
    facebookLoginButton.setReadPermissions("public_profile", "email");

    facebookLoginButton.registerCallback(mFacebookCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.e("callback", "success");
            new GraphRequest(AccessToken.getCurrentAccessToken(), "/me", null, HttpMethod.GET, new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    /* handle the result */
                    try {
                        getFacebookUserData(response.getJSONObject());
                    } catch (JSONException e) {
                        Log.e("graph request", "json exception");
                        e.printStackTrace();
                    }
                }
            }
            ).executeAndWait(); //<<<<----- problem is here!!! :(
            Log.e("graph request", "executed");
            uploadUserToServer();
            startLaunchActivity();
        }

        @Override
        public void onCancel() { }

        @Override
        public void onError(FacebookException exception) {
            Log.e("callback", "error");
            exception.printStackTrace();}

    });

}

      

This is my onActivityResult () method for callbackManager.

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
    mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);

}

      

There are other methods in my code that I did not show, but they do other things that are not relevant. This code also does not contain the google + sign which adds a layer of complexity with it onConnectedMethod()

. All I want is GraphRequest execution!

here is the log

07-22 11:01:54.784  14078-14078/com.deepomatic.dream E/callback﹕ success
07-22 11:01:54.847  14078-14078/com.deepomatic.dream D/dalvikvm﹕ GC_FOR_ALLOC freed 378K, 4% free 9993K/10408K, paused 46ms, total 48ms
07-22 11:01:54.870  14078-14078/com.deepomatic.dream D/AndroidRuntime﹕ Shutting down VM
07-22 11:01:54.870  14078-14078/com.deepomatic.dream W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416b7700)
07-22 11:01:54.901  14078-14078/com.deepomatic.dream E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.deepomatic.dream/com.deepomatic.dream.LoginActivity2}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
        at android.app.ActivityThread.access$1100(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
        at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:708)
        at java.io.OutputStream.write(OutputStream.java:82)
        at libcore.net.http.HttpEngine.writeRequestHeaders(HttpEngine.java:665)
        at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:814)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
        at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
        at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
        at com.facebook.GraphResponse.fromHttpConnection(GraphResponse.java:253)
        at com.facebook.GraphRequest.executeConnectionAndWait(GraphRequest.java:1130)
        at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:1032)
        at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:999)
        at com.facebook.GraphRequest.executeBatchAndWait(GraphRequest.java:983)
        at com.facebook.GraphRequest.executeAndWait(GraphRequest.java:958)
        at com.facebook.GraphRequest.executeAndWait(GraphRequest.java:854)
        at com.deepomatic.dream.LoginActivity2$1.onSuccess(LoginActivity2.java:91)
        at com.deepomatic.dream.LoginActivity2$1.onSuccess(LoginActivity2.java:76)
        at com.facebook.login.LoginManager.finishLogin(LoginManager.java:508)
        at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:192)
        at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
        at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
        at com.deepomatic.dream.LoginActivity2.onActivityResult(LoginActivity2.java:114)
        at android.app.Activity.dispatchActivityResult(Activity.java:5322)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
at  android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)            

      

+3


source to share


1 answer


Use this code to login to facebook. This works for me.

callbackManager = CallbackManager.Factory.create();

loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
	"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
	public void onSuccess(LoginResult loginResult) {

		System.out.println("onSuccess");

		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("LoginActivity",
				response.toString());
				try {
					String id = object.getString("id");
					try {
						URL profile_pic = new URL(
							"http://graph.facebook.com/" + id + "/picture?type=large");
						Log.i("profile_pic",
						profile_pic + "");

					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					String name = object.getString("name");
					String email = object.getString("email");
					String gender = object.getString("gender");
					String birthday = object.getString("birthday");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		Bundle parameters = new Bundle();
		parameters.putString("fields",
			"id,name,email,gender, birthday");
		request.setParameters(parameters);
		request.executeAsync();
	}

	@Override
	public void onCancel() {
		System.out.println("onCancel");
	}

	@Override
	public void onError(FacebookException exception) {
		System.out.println("onError");
		Log.v("LoginActivity", exception.getCause().toString());
	}
});
      

Run codeHide result




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

Run codeHide result


0


source







All Articles