0

我能夠爲我爲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(); 
    } 
} 
+0

當你來自Facebook SDK的用戶數據並解析該json對象,並在本地數據庫或Web上保存您想要的內容。你面臨的問題是什麼? –

+0

您可以簡單地解析json並將其存儲到任何地方。 –

+0

現在,我想將它保存到本地數據庫中,因爲我在應用程序上工作。我正在考慮MySQL本地數據庫。 –

回答

0

當你在響應完成回調簡單解析這樣

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的數據都存儲在一個profileBean所以這樣你可以得到你的數據..

+0

是的,我這樣做是爲了保存用戶數據並將其顯示在配置文件頁面上,但如果我希望另一個用戶查看該配置文件信息,該怎麼辦。 –

+0

爲此,您必須將此數據存儲到本地存儲。爲此,你可以使用共享偏好或本地數據庫任何人..但我寧願將數據存儲在本地數據庫.. –

+0

我應該使用什麼樣的本地數據庫? MySQL的? SQLite的? –

相關問題