1

将 facebook sdk 集成到我的 android 应用程序。我现在可以成功登录我正在尝试获取用户 ID 和个人资料图片和名称。但是在这里String jsonUser = fb.request("me");我收到 facebook 错误(通过调试尝试,执行 facebookerror catch 块)。如何解决这个问题. 在这里,我在会话有效后放置代码。

if (fb.isSessionValid()) {
            button.setImageResource(R.drawable.logout_button);
            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");
                hello.setText("Welcome, " + name);
                img_url = new URL("http://graph.facebook.com/" + id
                        + "/picture?type=normal");
                Bitmap bmp = BitmapFactory.decodeStream(img_url
                        .openConnection().getInputStream());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FacebookError 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 {
            button.setImageResource(R.drawable.login_button);
            pic.setVisibility(ImageView.INVISIBLE);
        }
    }
4

1 回答 1

0

登录后调用此方法...

getProfileInformation();

在添加以下代码后..

public void getProfileInformation() {

        mAsyncRunner.request("me", new RequestListener() {

            public void onComplete(String response, Object state) {
                Log.d("Profile", response);
                String json = response;
                try {
                    JSONObject profile = new JSONObject(json);                  
                    // getting name of the user
                    final String name = profile.getString("name");
                    // getting email of the user
                    final String email = profile.getString("email");
                        // getting email of the user
                    final String id = profile.getString("id");

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email +"\nId: " + id, Toast.LENGTH_LONG).show();

                        }

                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onIOException(IOException e, Object state) {
            }

            public void onFileNotFoundException(FileNotFoundException e,
                    Object state) {
            }

            public void onMalformedURLException(MalformedURLException e,
                    Object state) {
            }

            public void onFacebookError(FacebookError e, Object state) {
            }
        });


    }

public static AsyncFacebookRunner mAsyncRunner = null;

mAsyncRunner = new AsyncFacebookRunner(fb);

于 2013-08-17T09:04:41.790 回答