我是 facebook api 的新手。我尝试了多个教程,但对我没有任何效果...我使用的是 api 级别 10,平台 2.3.3,连接工作正常,但是当我注销并再次打开应用程序时,它应该向我显示带有我名字的欢迎文本而是应用程序崩溃...
编码:
package com.example.kamehameha;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.*;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.facebook.model.*;
import com.parse.Parse;
import com.parse.ParseFacebookUtils;
import com.parse.ParseObject;
public class MainActivity extends Activity {
Facebook fb;
private SharedPreferences sp;
TextView welcome;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String APP_ID = getString(R.string.app_id);
fb = new Facebook(APP_ID);
welcome = (TextView)findViewById(R.id.welcome);
sp = getPreferences(MODE_PRIVATE);
String at = sp.getString("access_token", null);
long expires = sp.getLong("access_expires", 0);
if(at != null){
fb.setAccessToken(at);
}
if(expires != 0){
fb.setAccessExpires(expires);
}
getName();
}
@SuppressWarnings("deprecation")
public void getName(){
JSONObject obj = null;
welcome = (TextView)findViewById(R.id.welcome);
if(fb.isSessionValid()){
try {
String jsonUser = fb.request("me");
obj = Util.parseJson(jsonUser);
String name = obj.optString("name");
welcome.setText("Welcome"+name);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
fb.authorize(this, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "fbError",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onError",Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor = sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onCancel",Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
fb.authorizeCallback(requestCode, resultCode, data);
}
}
我在 logcat 中得到什么:
02-05 01:10:41.515: E/AndroidRuntime(30114): FATAL EXCEPTION: main
02-05 01:10:41.515: E/AndroidRuntime(30114): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kamehameha/com.example.kamehameha.MainActivity}: android.os.NetworkOnMainThreadException
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.ActivityThread.access$600(ActivityThread.java:128)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.os.Looper.loop(Looper.java:137)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.ActivityThread.main(ActivityThread.java:4514)
02-05 01:10:41.515: E/AndroidRuntime(30114): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 01:10:41.515: E/AndroidRuntime(30114): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
02-05 01:10:41.515: E/AndroidRuntime(30114): at dalvik.system.NativeStart.main(Native Method)
02-05 01:10:41.515: E/AndroidRuntime(30114): Caused by: android.os.NetworkOnMainThreadException
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-05 01:10:41.515: E/AndroidRuntime(30114): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
02-05 01:10:41.515: E/AndroidRuntime(30114): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
02-05 01:10:41.515: E/AndroidRuntime(30114): at java.net.InetAddress.getAllByName(InetAddress.java:220)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-05 01:10:41.515: E/AndroidRuntime(30114): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.facebook.android.Util.openUrl(Util.java:219)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.facebook.android.Facebook.requestImpl(Facebook.java:806)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.facebook.android.Facebook.request(Facebook.java:732)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.example.kamehameha.MainActivity.getName(MainActivity.java:71)
02-05 01:10:41.515: E/AndroidRuntime(30114): at com.example.kamehameha.MainActivity.onCreate(MainActivity.java:58)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.Activity.performCreate(Activity.java:4562)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
02-05 01:10:41.515: E/AndroidRuntime(30114): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
02-05 01:10:41.515: E/AndroidRuntime(30114): ... 11 more