我正在 android 上编写一个简单的应用程序来获取通过 Facebook 登录该应用程序的用户的信息。这是我的代码:
public class example extends Activity {
Facebook facebook = new Facebook("id");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
private static final String TAG ="Debug";
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
/*
* Get existing access_token if any
*/
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null) {
facebook.setAccessToken(access_token);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {
facebook.authorize(this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"}, new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
}
Log.d(TAG, access_token);
getInformation();
}
private void getInformation(){
JSONObject json = null;
try
{
Log.d("testing", "point1");
String response = facebook.request("me");
Log.d("testing", "point2");
json = Util.parseJson(response);
String id = json.getString("id");
Log.d("testing", id);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (FacebookError e)
{
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
@Override
public void onResume() {
super.onResume();
facebook.extendAccessTokenIfNeeded(this, null);
}
}
我在这里关注https://developers.facebook.com/docs/mobile/android/build/#sdk并使用相同的 facebook SDK。所以如代码所示,在 logcat 中只有 point1 而不是 point2,这意味着 facebook.request("me") 不成功。模拟器显示应用程序关闭并说“不幸的是,示例已停止”。
那么有人可以帮我指出这个问题吗?我想知道访问令牌中是否有问题,但我不知道。非常感谢。
日志猫:
11-03 10:13:55.986: D/Debug(893): AAAFi20EvlJEBACwBLIR4buSOaZB1emxACFPZCZAKums6ZCNwZB0wZCvQnI3grshT0YNl8ZC427BSkrNye6akdCZBjvp4KL24Oyx......
11-03 10:13:55.986: D/testing(893): point1
11-03 10:13:56.106: D/AndroidRuntime(893): Shutting down VM
11-03 10:13:56.106: W/dalvikvm(893): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-03 10:13:56.136: E/AndroidRuntime(893): FATAL EXCEPTION: main
11-03 10:13:56.136: E/AndroidRuntime(893): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crush/com.example.crush.Crush}: android.os.NetworkOnMainThreadException
11-03 10:13:56.136: E/AndroidRuntime(893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)