簡體   English   中英

如何在android應用程序中使用facebook sdk獲取用戶名和圖像

[英]How to get user name and image using facebook sdk in android app

將 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);
        }
    }

登錄后調用此方法...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM