1

我試圖獲得在Facebook Android SDK中的用戶信息..不是從<code>Request.newMeRequest(session, new Request.GraphUserCallback()</code>這是從Android SDK中經理更新的功能,工作

我沒有得到結果的Facebook的Android代碼

public class MainFragment extends Fragment { 

    private static final String TAG = "MainFragment"; 
    private UiLifecycleHelper uiHelper; 
    private TextView userInfoTextView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.main, container, false); 
     LoginButton authButton = (LoginButton) view 
       .findViewById(R.id.authButton); 

     authButton.setFragment(this); 
     authButton.setReadPermissions(Arrays.asList("user_location", 
       "user_birthday", "user_likes")); 
     userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView); 

     return view; 
    } 

    private void onSessionStateChange(Session session, SessionState state, 
      Exception exception) { 
     if (state.isOpened()) { 
      userInfoTextView.setVisibility(View.VISIBLE); 
      Settings.addLoggingBehavior(LoggingBehavior.REQUESTS); 
      Request.newMeRequest(session, new Request.GraphUserCallback() { 

       @Override 
       public void onCompleted(GraphUser user, Response response) { 
        // TODO Auto-generatedation I put the tags below in wrong 
        // places (I method stub 
        if (user != null) { 
         // Display the parsed user info 
         Toast.makeText(null, "Entered the user section", 
           Toast.LENGTH_LONG).show(); 

         userInfoTextView.setText(buildUserInfoDisplay(user)); 
        } else if (user == null) { 
         Toast.makeText(null, "User is null error in request", 
           Toast.LENGTH_LONG).show(); 

        } 

       } 
      }).executeAsync(); 

      Log.i(TAG, "Logged in..."); 
     } else if (state.isClosed()) { 
      userInfoTextView.setVisibility(View.INVISIBLE); 
      Log.i(TAG, "Logged out..."); 
     } 
    } 

    private Session.StatusCallback callback = new Session.StatusCallback() { 
     @Override 
     public void call(Session session, SessionState state, 
       Exception exception) { 
      onSessionStateChange(session, state, exception); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     uiHelper = new UiLifecycleHelper(getActivity(), callback); 
     uiHelper.onCreate(savedInstanceState); 

    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     Session session = Session.getActiveSession(); 
     if (session != null && (session.isOpened() || session.isClosed())) { 
      onSessionStateChange(session, session.getState(), null); 
     } 

     uiHelper.onResume(); 
    } 

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

    @Override 
    public void onPause() { 
     super.onPause(); 
     uiHelper.onPause(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     uiHelper.onDestroy(); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     uiHelper.onSaveInstanceState(outState); 
    } 

    private String buildUserInfoDisplay(GraphUser user) { 
     StringBuilder userInfo = new StringBuilder(""); 

     userInfo.append(String.format("Name: %s\n\n", user.getName())); 
     userInfo.append(String.format("Birthday: %s\n\n", user.getBirthday())); 
     userInfo.append(String.format("Location: %s\n\n", user.getLocation() 
       .getProperty("name"))); 
     userInfo.append(String.format("Locale: %s\n\n", 
       user.getProperty("locale"))); 
     // JSONArray languages = (JSONArray)user.getProperty("languages"); 
     GraphObjectList<MyGraphLanguage> languages = (user 
       .cast(MyGraphUser.class)).getLanguages(); 
     if (languages.size() > 0) { 
      ArrayList<String> languageNames = new ArrayList<String>(); 

      for (MyGraphLanguage language : languages) { 
       languageNames.add(language.getName()); 
      } 
      userInfo.append(String.format("Languages: %s\n\n", 
        languageNames.toString())); 
     } 

     return userInfo.toString(); 
    } 

    private interface MyGraphLanguage extends GraphObject { 
     // Getter for the ID field 
     String getId(); 

     // Getter for the Name field 
     String getName(); 
    } 

    private interface MyGraphUser extends GraphUser { 
     // Create a setter to enable easy extraction of the languages field 
     GraphObjectList<MyGraphLanguage> getLanguages(); 
    } 
} 

回答

0

您可以嘗試使用簡單的下列代碼。

public class Profile_Detail extends Activity implements OnClickListener { 

    ImageView profile_pic; 

    TextView mfirstname, mlastname, mfullname, mfacebookLink, mfacebookId, 
      mfacebookUnm, mgender, mEmail; 

    private Session.StatusCallback statusCallback = new SessionStatusCallback(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.profile); 

     Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); 
     Session session = Session.getActiveSession(); 
     if (session == null) { 
      if (savedInstanceState != null) { 
       session = Session.restoreSession(this, null, statusCallback, 
         savedInstanceState); 
      } 
      if (session == null) { 
       session = new Session(this); 
      } 
      Session.setActiveSession(session); 
      if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { 
       session.openForRead(new Session.OpenRequest(this) 
         .setCallback(statusCallback)); 
      } 
     } 

     profile_pic = (ImageView) findViewById(R.id.profile_pic); 
     mfirstname = (TextView) findViewById(R.id.textView1); 
     mlastname = (TextView) findViewById(R.id.textView2); 
     mfullname = (TextView) findViewById(R.id.textView3); 
     mfacebookLink = (TextView) findViewById(R.id.textView4); 
     mfacebookId = (TextView) findViewById(R.id.textView5); 
     mfacebookUnm = (TextView) findViewById(R.id.textView6); 
     mgender = (TextView) findViewById(R.id.textView7); 
     mEmail = (TextView) findViewById(R.id.textView8); 

     Session.openActiveSession(this, true, new Session.StatusCallback() { 

      @Override 
      public void call(Session session, SessionState state, 
        Exception exception) { 
       if (session.isOpened()) { 
        // make request to the /me API 
        Request.executeMeRequestAsync(session, 
          new Request.GraphUserCallback() { 
           @Override 
           public void onCompleted(GraphUser user, 
             Response response) { 
            if (user != null) { 
             mfirstname.setText(user.getFirstName()); 
             mlastname.setText(user.getLastName()); 
             mfullname.setText(user.getName()); 
             mfacebookLink.setText(user.getLink()); 
             mfacebookId.setText(user.getId()); 
             mfacebookUnm.setText(user.getUsername() 
               + " (" + user.getName() + ")"); 
             String gender = user.asMap() 
               .get("gender").toString(); 
             String email = user.asMap() 
               .get("email").toString(); 
             mgender.setText(gender); 
             mEmail.setText(email); 

             StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
               .permitAll().build(); 
             StrictMode.setThreadPolicy(policy); 

             try { 
              URL image_value = new URL(
                "http://graph.facebook.com/" 
                  + user.getId() 
                  + "/picture?type=large"); 
              Bitmap bmp = null; 
              try { 
               bmp = BitmapFactory 
                 .decodeStream(image_value 
                   .openConnection() 
                   .getInputStream()); 
              } catch (IOException e) { 
               e.printStackTrace(); 
              } 
              profile_pic.setImageBitmap(bmp); 
             } catch (MalformedURLException e) { 
              e.printStackTrace(); 
             } 
            } 
           } 
          }); 
       } else { 
        Toast.makeText(getApplicationContext(), "Error...", 
          Toast.LENGTH_LONG); 
       } 
      } 
     }); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Session.getActiveSession().addCallback(statusCallback); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Session.getActiveSession().removeCallback(statusCallback); 
    } 

    private class SessionStatusCallback implements Session.StatusCallback { 
     @Override 
     public void call(Session session, SessionState state, 
       Exception exception) { 

     } 
    } 
} 

有了上面的代碼,我成功地得到了facebook用戶的詳細信息以及個人資料圖片。