如何在Android应用程序中使用Facebook sdk获取用户名和图像

时间:2013-08-17 08:08:33

标签: android facebook-graph-api

将facebook sdk集成到我的Android应用程序.i现在能够成功登录我正在尝试获取用户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);
        }
    }

1 个答案:

答案 0 :(得分: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);