这是我用于 Facebook 登录的 MainLogin.java 活动编码
public class MainLogin extends Activity implements OnClickListener{
Button login;
ImageView pic;
Facebook fb;
TextView welcome;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_login);
String APP_ID = getString(R.string.APP_ID);
fb = new Facebook(APP_ID);
login = (Button) findViewById(R.id.facebook_login);
pic = (ImageView) findViewById(R.id.user_pic);
welcome = (TextView) findViewById(R.id.welcome);
updateLoginState();
}
public void updateLoginState() {
if(fb.isSessionValid()){
Toast.makeText(this, "Logged In successflly2", Toast.LENGTH_SHORT).show();
pic.setVisibility(ImageView.VISIBLE);
JSONObject obj = null;
URL img_url = null;
try {
String jsonUser = fb.request("me");
obj = Util.parseJson(jsonUser);
String id = obj.optString("id");
String name = obj.optString("name");
welcome.setText("Welcome"+name);
img_url = new URL("http://graph.facebook.com/"+id+"/picture?type=small");
Bitmap bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
pic.setImageBitmap(bmp);
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
pic.setVisibility(ImageView.INVISIBLE);
}
}
public void onClick(View v) {
switch(v.getId()){
case R.id.facebook_login:
if(fb.isSessionValid()){
try {
fb.logout(getApplicationContext());
updateLoginState();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
fb.authorize(MainLogin.this, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
}
@Override
public void onError(DialogError e) {
Toast.makeText(getApplicationContext(), "error",
Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Bundle values) {
updateLoginState();
Toast.makeText(getApplicationContext(), "Logged in Successfully!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Canceld",
Toast.LENGTH_SHORT).show();
}
});
}
break;
}
}
}
此应用程序登录成功,但出现updateLoginState()
以下错误。这是我的日志猫
08-15 15:26:43.474: E/AndroidRuntime(16241): android.os.NetworkOnMainThreadException
08-15 15:26:43.474: E/AndroidRuntime(16241): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
08-15 15:26:43.474: E/AndroidRuntime(16241): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-15 15:26:43.474: E/AndroidRuntime(16241): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-15 15:26:43.474: E/AndroidRuntime(16241): at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:185)
08-15 15:26:43.474: E/AndroidRuntime(16241): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.android.Util.openUrl(Util.java:219)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.android.Facebook.requestImpl(Facebook.java:806)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.android.Facebook.request(Facebook.java:732)
08-15 15:26:43.474: E/AndroidRuntime(16241): at in.blogspot.pcnlap.sha_main_stream.MainLogin.updateLoginState(MainLogin.java:77)
08-15 15:26:43.474: E/AndroidRuntime(16241): at in.blogspot.pcnlap.sha_main_stream.MainLogin.access$1(MainLogin.java:68)
08-15 15:26:43.474: E/AndroidRuntime(16241): at in.blogspot.pcnlap.sha_main_stream.MainLogin$1.onComplete(MainLogin.java:145)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.android.Facebook.onSessionCallback(Facebook.java:345)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.android.Facebook.access$11(Facebook.java:326)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.android.Facebook$1.call(Facebook.java:304)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.facebook.Session$3$1.run(Session.java:1190)
08-15 15:26:43.474: E/AndroidRuntime(16241): at android.os.Handler.handleCallback(Handler.java:615)
08-15 15:26:43.474: E/AndroidRuntime(16241): at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 15:26:43.474: E/AndroidRuntime(16241): at android.os.Looper.loop(Looper.java:153)
08-15 15:26:43.474: E/AndroidRuntime(16241): at android.app.ActivityThread.main(ActivityThread.java:5086)
08-15 15:26:43.474: E/AndroidRuntime(16241): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 15:26:43.474: E/AndroidRuntime(16241): at java.lang.reflect.Method.invoke(Method.java:511)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-15 15:26:43.474: E/AndroidRuntime(16241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-15 15:26:43.474: E/AndroidRuntime(16241): at dalvik.system.NativeStart.main(Native Method)
当我尝试获取用户的个人资料图像时,updateLoginState()
应用程序本身崩溃,但此应用程序成功登录到 facebook,这里我一直在使用不推荐使用的方法。如果有任何好的和简单的教程可用于 facebook 登录和更多的东西然后建议我