gpt4 book ai didi

android - SharedPreferences 无法保存数据

转载 作者:行者123 更新时间:2023-11-29 00:08:52 25 4
gpt4 key购买 nike

当我尝试在 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

最佳答案

将您的代码放在 onSuccess 方法中。所以你可以获得用户数据,因为如果用户没有成功登录,你就无法从响应中获得任何 ser deta。

为 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());
}
});

关于android - SharedPreferences 无法保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com