1

首先,对不起我糟糕的英语。

我正在尝试创建一个使用官方 Facebook API 连接到 Facebook 的简单 android 应用程序。当用户登录并授权我的应用程序时,我想获取他/她的用户名和头像,然后显示在我的应用程序中。我采用了 Facebook API 附带的 Hackbook 示例应用程序中的一些代码。这是我在主要活动类中的代码。

public class SocialApp2Activity extends Activity {
    private Handler handler;

    private Facebook facebook;
    private AsyncFacebookRunner asyncRunner;

    private TextView text;
    private ImageView imgUserPic;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        handler = new Handler();

        text = (TextView) findViewById(R.id.txt);
        imgUserPic = (ImageView) findViewById(R.id.user_pic);

        facebook = new Facebook("MY_FB_APP_ID");
        asyncRunner = new AsyncFacebookRunner(facebook);

        facebook.authorize(this, new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
                Bundle params = new Bundle();
                params.putString("fields", "name, picture");
                asyncRunner.request("me", params, new UserRequestListener());
            }

            ... 

        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        facebook.authorizeCallback(requestCode, resultCode, data);
    }


    /* Callback for fetching username and profile picture */
    public class UserRequestListener implements RequestListener {

        @Override
        public void onComplete(final String response, final Object state) {

            JSONObject jsonObject;

            try {
                jsonObject = new JSONObject(response);

                final String picURL = jsonObject.getString("picture");
                final String name = jsonObject.getString("name");

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        text.setText(name);

                        /********************************
                         *** This line cause an error ***
                         ********************************/
                        imgUserPic.setImageBitmap(Utility.getBitmap(picURL));

                    }
                });
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        ...
    }
}

我在调用我自己的从远程服务器读取个人资料图片位图数据的方法时出错。错误是 NetworkOnMainThreadException 并且这里有一些堆栈跟踪。

05-04 14:37:28.898: W/System.err(1117): android.os.NetworkOnMainThreadException
05-04 14:37:28.905: W/System.err(1117):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
05-04 14:37:28.905: W/System.err(1117):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
...
05-04 14:37:29.005: W/System.err(1117):     at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164)
05-04 14:37:29.005: W/System.err(1117):     at promlert.socialapp2.Utility.getBitmap(Utility.java:26)
05-04 14:37:29.016: W/System.err(1117):     at promlert.socialapp2.SocialApp2Activity$UserRequestListener$1.run(SocialApp2Activity.java:95)
...

您能否建议我如何解决此问题。谢谢

4

1 回答 1

1

handler.post(Runnable runnable) 正在 UI 线程上执行 runnable。所以如果你试图在 UI 线程上执行网络操作 - 你会得到这个异常。为避免这种情况:首先不从 UI 线程调用 Utility.getBitmap(picURL),然后仅在 UI 线程上发布 imgUserPic.setImageBitmap() 方法:

new Thread(new Runnable() {

                @Override
                public void run() {
                    //this line must be called not from the UI  thread
                final Bitmap bitmap = Utility.getBitmap(picURL)
                handler.post(new Runnable() {
                @Override
                public void run() {
                                   text.setText(name);
                                   imgUserPic.setImageBitmap(bitmap );

                                  }
                  });   
                }
}).start();
于 2012-05-04T09:41:33.540 回答