当我尝试在SharedPreferences中保存数据时出现问题。 为什么我不能正常将数据保存到SharedPreferences中? 我已经尝试过几次,但只有SharedPreferences在另一个类中工作。 我也知道我做不到this。
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
// TODO Auto-generated method stub
try {
System.out.println("object : " + object.toString());
String id = object.getString("id");
System.out.println("id" + id);
String name = object.getString("name");
System.out.println("name" + name);
String email = object.getString("email");
System.out.println("email" + email);
String gender = object.getString("gender");
System.out.println("gender" + gender);
String birthday = object.getString("birthday");
System.out.println("birthday" + birthday);
JSONObject json_data = ((JSONObject) object).getJSONObject("picture").getJSONObject("data");
String fb_picture_url = json_data.getString("url");
URL FBPictureURL = new URL(fb_picture_url);
System.out.println("profile picture uri" + fb_picture_url);
SharedPreferences abc = getSharedPreferences("FBProfile",0);
SharedPreferences.Editor editor = abc.edit();
editor.putString(PreferenceSettings.fb_id, id);
editor.putString(PreferenceSettings.fb_name, name);
editor.putString(PreferenceSettings.fb_email, email);
editor.putString(PreferenceSettings.fb_gender, gender);
editor.putString(PreferenceSettings.fb_birthday, birthday);
editor.putString(PreferenceSettings.fb_picture_url, fb_picture_url);
editor.commit();
System.out.println("name" + PreferenceSettings.fb_id);
System.out.println("name" + PreferenceSettings.fb_name);
System.out.println("name" + PreferenceSettings.FacebookState);
这里是PreferenceSettings的字符串格式,SharedPreferences没有保存。
public static String fb_id="",
fb_name="",
fb_gender="",
fb_birthday="",
fb_email="",
fb_picture_url="";
保存后记录没有变化..
07-31 07:20:56.681 9303-9303/? I/System.out﹕ name My Name
07-31 07:20:56.711 9303-9303/? I/System.out﹕ name
07-31 07:20:56.711 9303-9303/? I/System.out﹕ name
07-31 07:20:56.711 9303-9303/? I/System.out﹕ nameMailauthorizing
答案 0 :(得分:1)
将您的代码放入onSuccess方法中。因此,您可以获取用户数据因为如果用户未成功登录,则无法从响应中获得任何服务。
尝试使用SDK 4的代码。它已成功运行。我刚刚在你应该开始编写代码的地方做了评论。
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity", response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
// your SharedPreferences code starts from here.
SharedPreferences sharedpreferences = getSharedPreferences("myprf",Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putBoolean("login", true);
editor.putString("name", name);
editor.putString("email", email);
editor.putString("gender", gender);
editor.putString("birthday",birthday);
editor.commit();
Log.i("name", sharedpreferences.getString("name", ""));
Log.i("email", sharedpreferences.getString("email", ""));
Log.i("gender", sharedpreferences.getString("gender", ""));
Log.i("birthday", sharedpreferences.getString("birthday", ""));
// your SharedPreferences code ends here.
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
&#13;
答案 1 :(得分:1)
您可以使用此方法保存价值:
public void savePreferencesForReasonCode(Context context,
String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
使用此方法,您可以从SharedPreferences获取价值:
public String getPreferences(Context context, String prefKey) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
return sharedPreferences.getString(prefKey, "");
}
此处prefKey
是您用于保存特定值的键。
感谢。