如何使用 Facebook SDK 将 Facebook 用户个人资料信息存储在数据库中?



我能够为我正在为Android开发的社交网络应用程序实现Facebook登录。我使用Facebook的android sdk来做到这一点,以及获取名字/姓氏,性别和年龄,并将其显示在个人资料页面上。现在我想知道如何将此信息存储在数据库中,以便将其显示给访问配置文件的其他用户。提前感谢!这是我用于个人资料页面的代码。

import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.Profile;
import com.squareup.picasso.Picasso;
import org.json.JSONObject;

public class ProfileActivity extends AppCompatActivity {
private Profile profile = Profile.getCurrentProfile();
public ImageView profileActivityPicture;
public TextView nameFirst, nameLast, age, gend, user_location;
private String userID, email, gender, picture, birthday, name, city;
private String profilePicUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("My Profile");
}
nameFirst = (TextView) findViewById(R.id.profileActivityFirstName);
nameLast = (TextView) findViewById(R.id.profileActivityLastName);
age = (TextView) findViewById(R.id.profileActivityAge);
gend = (TextView) findViewById(R.id.profileActivityGender);
profileActivityPicture = (ImageView) findViewById(R.id.profileActivityPic);
user_location = (TextView) findViewById(R.id.profileActivityCity);
if(profile != null) {
nameFirst.setText(getString(R.string.hello_user, profile.getFirstName()));
nameLast.setText(getString(R.string.hello_user, profile.getLastName()));
}
nameFirst.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
nameFirst.setTypeface(null, Typeface.BOLD);
nameLast.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
nameLast.setTypeface(null, Typeface.BOLD);
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject data,
GraphResponse response) {
try {
data = response.getJSONObject();
if (data.has("id"))
userID = data.getString("id");
if (data.has("name"))
name = data.getString("name");
if (data.has("location"))
city = data.getJSONObject("location").getString("name");
user_location.setText(city);
user_location.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
if (data.has("picture"))
Picasso.with(ProfileActivity.this).load("https://graph.facebook.com/" + userID+ "/picture?width=3000&height=4000").into(profileActivityPicture);
if (data.has("birthday"))
birthday = data.getString("birthday");
age.setText(birthday);
age.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
age.setTypeface(null, Typeface.BOLD);
if (data.has("gender"))
gender = data.getString("gender");
gend.setText(gender);
gend.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
} catch(Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",  "id,name,email,gender,cover,picture.type(large),location");
request.setParameters(parameters);
request.executeAsync();
}

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
@Override
public void onDestroy(){
super.onDestroy();
}
}

当你在完成的回调中获得响应时,只需像这样解析

JSONObject jobj = response.getJSONObject();
if (jobj != null) {
try {
ProfileBean profileBean = new ProfileBean();
profileBean.id = jobj.getString("id");
profileBean.name = jobj.getString("name");
profileBean.email = jobj.getString("email");
profileBean.link = jobj.getString("link");
profileBean.last_name = jobj.getString("last_name");
profileBean.first_name = jobj.getString("first_name");
profileBean.gender = jobj.getString("gender");
JSONObject pic = jobj.getJSONObject("picture");
JSONObject data = pic.getJSONObject("data");
profileBean.profile_pic = data.getString("url");

} catch (Exception e) {
// print exception
}
}

在这里,您的FB数据存储在配置文件Bean中,因此您可以像这样获取数据。

相关内容

最新更新