Im пытается использовать facebook login, чтобы получить изображение профиля пользователя.Как войти в профиль пользователя. Facebook SDK 3.5
Im используя этот код, чтобы войти в 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();
}
}
}
В моем коде я пытаюсь получить профиль пользователя Pictrue, используя URL facebook дал здесь:
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");
Спасибо, что он работает :) Мне нужно знать, как я могу войти в идентификатор пользователя, вы можете мне помочь? – dasdasd
У вас есть класс AsyncFacebookRunner в вашем фейсбуке SDK .. – Hariharan
@Tamilan Does AsyncFacebookRunner заменить AsyncTask? –