我试图使用facebook登录来获取用户个人资料照片。
我使用此代码从示例Facebook代码登录到Facebook:
package com.facebook.samples.sessionlogin;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
public class LoginUsingActivityActivity extends Activity {
private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";
private TextView textInstructionsOrLink;
private Button buttonLoginLogout;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
buttonLoginLogout = (Button)findViewById(R.id.buttonLoginLogout);
textInstructionsOrLink = (TextView)findViewById(R.id.instructionsOrLink);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
updateView();
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
textInstructionsOrLink.setText(URL_PREFIX_FRIENDS + session.getAccessToken());
buttonLoginLogout.setText(R.string.logout);
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogout(); }
});
} else {
textInstructionsOrLink.setText(R.string.instructions);
buttonLoginLogout.setText(R.string.login);
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogin(); }
});
}
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
private void onClickLogout() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
}
}
}
在我的代码中,我尝试使用facebook在此处提供的URL获取用户个人资料pictrue:
https://developers.facebook.com/docs/reference/api/using-pictures/
在我的活动结果中,我有以下代码:
ImageView user_picture;
user_picture= (ImageView)findViewById(R.id.userpicture);
URL img_value = null;
try {
img_value = new URL("https://graph.facebook.com/shaverm/picture?type=normal");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap mIcon1 = null;
try {
mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
user_picture.setImageBitmap(mIcon1);
super.onActivityResult(requestCode, resultCode, data2);
但是我得到了这个错误:
08-23 15:08:59.705: E/AndroidRuntime(18684): FATAL EXCEPTION: main
08-23 15:08:59.705: E/AndroidRuntime(18684): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=64206, result=-1, data=Intent { (has extras) }} to activity {com.tomer.workoutlog2/com.example.workoutlog.DatePage}: android.os.NetworkOnMainThreadException
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.ActivityThread.deliverResults(ActivityThread.java:3446)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3489)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.ActivityThread.access$1100(ActivityThread.java:153)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.os.Looper.loop(Looper.java:137)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.ActivityThread.main(ActivityThread.java:5226)
08-23 15:08:59.705: E/AndroidRuntime(18684): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:08:59.705: E/AndroidRuntime(18684): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 15:08:59.705: E/AndroidRuntime(18684): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-23 15:08:59.705: E/AndroidRuntime(18684): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-23 15:08:59.705: E/AndroidRuntime(18684): at dalvik.system.NativeStart.main(Native Method)
08-23 15:08:59.705: E/AndroidRuntime(18684): Caused by: android.os.NetworkOnMainThreadException
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-23 15:08:59.705: E/AndroidRuntime(18684): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:699)
08-23 15:08:59.705: E/AndroidRuntime(18684): at java.io.OutputStream.write(OutputStream.java:82)
08-23 15:08:59.705: E/AndroidRuntime(18684): at libcore.net.http.HttpEngine.writeRequestHeaders(HttpEngine.java:659)
08-23 15:08:59.705: E/AndroidRuntime(18684): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:808)
08-23 15:08:59.705: E/AndroidRuntime(18684): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
08-23 15:08:59.705: E/AndroidRuntime(18684): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
08-23 15:08:59.705: E/AndroidRuntime(18684): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
08-23 15:08:59.705: E/AndroidRuntime(18684): at com.example.workoutlog.DatePage.onActivityResult(DatePage.java:3153)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.Activity.dispatchActivityResult(Activity.java:5293)
08-23 15:08:59.705: E/AndroidRuntime(18684): at android.app.ActivityThread.deliverResults(ActivityThread.java:3442)
08-23 15:08:59.705: E/AndroidRuntime(18684): ... 11 more
我也试过这个问题:
Android and Facebook: How to get picture of logged in User
此代码我被弃用了错误:
Facebook mFacebook = new Facebook(APP_ID);
// ... user logs in ...
//String jsonUser = mFacebook.request("me/picture"); // throws error
String jsonUser = mFacebook.request("me");
JSONObject obj = Util.parseJson(jsonUser);
String facebookId = obj.optString("id");
String name = obj.optString("name");
答案 0 :(得分:1)
您正在获取主线程异常的网络。因此,尝试在asynctask中执行您的过程或尝试使用
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
在onCreate()
; 之后的setContentView(R.layout.xxx)
中