这是我检索共享首选项中存储的信息的方法,然后比较用户名和密码是否存在。如果用户已登录,我可以显示其他活动。
SharedPreferences settings = getSharedPreferences(“logindetails”,0);
String username = settings.getString("username", "");
String password = settings.getString("password", "");
但是现在我正在尝试检索用户的用户名和性别并显示在我的活动中。
我正试图解决这个问题,但尚未找到令人信服的结果。
任何人都可以帮助我摆脱我的要求吗?
注意:我也看到Facebook开发者页面的文件
这是我用来检索用户数据并在另一个活动中显示的代码。面临的问题是在Facebook的响应被削弱之前加载布局。我怀疑这是因为使用了mAsyncRunner,因为它自己进行控制。我想要找出的是在加载布局之前解析它。
public class FaceBookRetrieval extends Activity{
this.facebookConnector = new FacebookConnect(APP_ID, this,
getApplicationContext(), PERMS);
}
public class FacebookConnect {
public FacebookConnect fb = null;
private Facebook facebook = null;
private Context context;
private String[] permissions;
public static final String TAG = "FACEBOOK";
private AsyncFacebookRunner mAsyncRunner;
private SharedPreferences sharedPrefs;
private ProgressBar pb;
public String fbName, fbGender;
private Activity activity;
public String check = "false";
private SessionListener mSessionListener = new SessionListener();;
public FacebookConnect(String appId, Activity activity, Context context,
String[] permissions) {
this.facebook = new Facebook(appId);
mAsyncRunner = new AsyncFacebookRunner(facebook);
SessionStore.restore(facebook, context);
SessionEvents.addAuthListener(mSessionListener);
SessionEvents.addLogoutListener(mSessionListener);
this.context = context;
this.permissions = permissions;
this.activity = activity;
}
public void login() {
if (!facebook.isSessionValid()) {
facebook.authorize(this.activity, this.permissions,
new LoginDialogListener());
}
}
public void getID() {
login();
return;
}
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
facebook.setAccessToken(null);
facebook.setAccessExpires(-1);
if (access_token != null && expires != -1) {
// facebook.setAccessToken(access_token);
// facebook.setAccessExpires(expires);
}
return facebook.isSessionValid();
}
// request the facebook to provide the response and then parse the response to
// obtain username and gender
private class IDRequestListener implements RequestListener {
@Override
public void onComplete(String response, Object state) {
try {
Log.d(TAG, "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
fbGender = json.getString("gender");
fbName = json.getString("name");
check = "true";
} catch (JSONException e) {
Log.d(TAG, "JSONException: " + e.getMessage());
} catch (FacebookError e) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
FacebookConnect.this.runOnUiThread(new Runnable() {
public void run() {
pb.setVisibility(ProgressBar.VISIBLE);
}
});
return ;
}
@Override
public void onIOException(IOException e, Object state) {
Log.d(TAG, "IOException: " + e.getMessage());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.d(TAG, "FacebookError: " + e.getMessage());
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
facebook.authorizeCallback(requestCode, resultCode, data);
}
public void runOnUiThread(Runnable runnable) {
}
private class LoginDialogListener implements DialogListener {
@Override
public void onComplete(Bundle values) {
String token = facebook.getAccessToken();
long token_expires = facebook.getAccessExpires();
Log.d(TAG, "AccessToken: " + token);
Log.d(TAG, "AccessExpires: " + token_expires);
sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
sharedPrefs.edit().putLong("access_expires", token_expires).clear()
.commit();
sharedPrefs.edit().putString("access_token", token).clear().commit();
mAsyncRunner.request("me", new IDRequestListener());
Toast.makeText(context, "You are logged in", Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onCancel() {
Log.d(TAG, "OnCancel");
}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
}
private class SessionListener implements AuthListener, LogoutListener {
public void onAuthSucceed() {
SessionStore.save(facebook, context);
}
public void onAuthFail(String error) {
}
public void onLogoutBegin() {
}
public void onLogoutFinish() {
SessionStore.clear(context);
}
}
public Facebook getFacebook() {
return this.facebook;
}
}
有人可以帮助我吗?
答案 0 :(得分:7)
最后我能够自己解决问题。
private static final String[] PERMS = new String[] { "publish_stream" };
----->
this.facebookConnector = new FacebookConnect(APP_ID, this, getApplicationContext(), PERMS);
---->
JSONObject json = Util.parseJson(response);
fbGender = json.getString("gender");
fbName = json.getString("name");
这是我的代码如何提取数据