0

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 と Facebook: ログインしているユーザーの写真を取得する方法

このコードに対して非推奨のエラーが発生します。

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");
4

1 に答える 1