我能夠爲我爲Android開發的社交網絡應用實施Facebook登錄。我使用Facebook的android sdk來做到這一點,以及獲取姓名,性別和年齡並在個人資料頁面上顯示。現在我想知道如何將這些信息存儲在數據庫中,以便將其顯示給訪問該配置文件的其他用戶。提前致謝!這是我用於配置文件頁面的代碼。如何使用Facebook sdk將Facebook用戶配置文件信息存儲在數據庫上?
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();
}
}
當你來自Facebook SDK的用戶數據並解析該json對象,並在本地數據庫或Web上保存您想要的內容。你面臨的問題是什麼? –
您可以簡單地解析json並將其存儲到任何地方。 –
現在,我想將它保存到本地數據庫中,因爲我在應用程序上工作。我正在考慮MySQL本地數據庫。 –