【发布时间】:2014-03-19 22:20:42
【问题描述】:
我正在开发应用程序,用户可以在其中注册,自行填写所有必需的信息,或添加电子邮件、Facebook 个人资料图片等信息。
我遵循了这个教程:https://parse.com/tutorials/integrating-facebook-in-android
到目前为止,当用户点击 Facebook 登录按钮 eaven 个人资料图片时,我已经完成了所有输入字段的填写。但是要创建新的 parseUser 我需要插入所有这样的值
ParseUser newUser = new ParseUser();
newUser.setUsername(email);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.put("name", name);
newUser.put("phone", phone);
要向用户添加个人资料图片,我需要获取 Byte[] 并将其放入 newUser。但我不知道如何从 facebookId 到 Byte[] 因为在本教程中我从 facebookId 获得了 facebook 用户的个人资料图片。
我提出请求的代码示例:
private void makeRequest() {
Request request = Request.newMeRequest(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
JSONObject userProfile = new JSONObject();
try {
userProfile.put("facebookId", user.getId());
userProfile.put("name", user.getName());
if (user.getLocation().getProperty("name") != null) {
userProfile.put("location", (String) user.getLocation().getProperty("name"));
}
if (user.getProperty("email") != null) {
userProfile.put("email",(String) user.getProperty("email"));
}
if (user.getBirthday() != null) {
userProfile.put("birthday",user.getBirthday());
}
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
updateprofileinfo();
} catch (JSONException e) {
Toast.makeText(FBLogin.this, "json error ", Toast.LENGTH_LONG).show();
}
} else if (response.getError() != null) {
if ((response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_RETRY)
|| (response.getError().getCategory() == FacebookRequestError.Category.AUTHENTICATION_REOPEN_SESSION)) {
Toast.makeText(FBLogin.this, "invalid fb session", Toast.LENGTH_LONG).show();
startLoginActivity();
} else {
Toast.makeText(FBLogin.this, "error", Toast.LENGTH_LONG).show();
}
}
}
});
request.executeAsync();
}
这是我从 facebook 获取信息并填写所有字段的代码:
private void updateprofileinfo() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.get("profile") != null) {
JSONObject userProfile = currentUser.getJSONObject("profile");
try {
if (userProfile.getString("facebookId") != null) {
String facebookId = userProfile.get("facebookId").toString();
mFbFoto.setProfileId(facebookId);
} else {
mFbFoto.setProfileId(null);
}
if (userProfile.getString("name") != null) {
mFbName.setText(userProfile.getString("name"));
} else {
mFbName.setText("");
}
if (userProfile.getString("email") != null) {
mFbEmail.setText(userProfile.getString("email"));
}
else {
mFbEmail.setText("");
}
}catch (JSONException e) {
Toast.makeText(FBLogin.this, "error update info", Toast.LENGTH_LONG).show();
}
}
}
如您所见,我从请求 facebookId 中获取并将其插入到 profilepicture 小部件中,但要上传此图片,我需要将其转换为 byte[],然后从 byte[] 创建新的 ParseFile,然后将其添加到 newUser 代码中,但我不知道如何从 facebookId 制作 byte[]。
我们将不胜感激。谢谢
【问题讨论】:
标签: android facebook facebook-graph-api parse-platform