我刚开始学习Android,正在构建一个需要登陆的应用。目前为止,Google+和普通电子邮件登陆都可以使用,但是我在Facebook登陆上遇到了麻烦。我大部分的代码都来自Facebook开发者文档,并且我已经尝试了多种方法来解决这个问题,但都没有成功。
以下是我的onCreate方法,其中我设置了callbackManager和loginButton,loginButton注册了回调函数。如果成功了,它会启动一个图形请求。然而,executeAndWait()方法从来没有工作过。
这是我的onActivityResult()方法,用于回调管理器。
以下是我的onCreate方法,其中我设置了callbackManager和loginButton,loginButton注册了回调函数。如果成功了,它会启动一个图形请求。然而,executeAndWait()方法从来没有工作过。
@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();}
});
}
这是我的onActivityResult()方法,用于回调管理器。
@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);
}
我的代码中还有其他的方法,但它们做的事情与此不相关。这个代码也没有包括 Google+ 登录,它通过其 onConnectedMethod()
增加了一层难度。我只想要执行 GraphRequest!
以下是日志:
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)