facebook个人资料图片抓取错误

时间:2013-08-15 11:57:48

标签: android facebook facebook-login

这是我登录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()后续错误。这是我的logcat

    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登录和更多的建议我

2 个答案:

答案 0 :(得分:0)

您正在尝试在UI线程上获取图像。这不符合Android标准。例如,您应该使用Async任务。

在此处查找信息: http://developer.android.com/reference/android/os/AsyncTask.html

或使用此课程:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
    this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}}

答案 1 :(得分:0)

您看到的是NetworkOnMainThreadException,这是因为您在主线程上运行了异步操作。 Android不允许这样做。

使用AsyncTask实现此功能或为您的应用启用StrickMode。