我是Android和Java开发的新手,我正在尝试找出一种方法来检索当前用户的个人资料帖子,并将其保存在我的智能手机上的文本文件中,以便进一步处理。
我可以检索用户名,电子邮件,性别,年龄范围和生日。到目前为止一切正常,当Facebook与我的应用程序连接时,正在编写文本文件。
下面的类是如何编码以检索提到的数据,如果有人知道如何通过这种方法检索帖子,请建议?
谢谢!
package com.example.daunte_pc.anique;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
public class SocialActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private static final String TAG = "SocialActivity";
LoginButton loginButton;
CallbackManager callbackManager;
ProgressDialog progressDialog;
URL profile_pic;
String id;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_social);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
NavigationView navigationView = (NavigationView)findViewById(R.id.navigation);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
FacebookSdk.sdkInitialize(getApplicationContext());
loginButton = (LoginButton)findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_status","user_posts"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
progressDialog = new ProgressDialog(SocialActivity.this);
progressDialog.setMessage("Please Wait! Anique_Profile's being processed!");
progressDialog.show();
String accessToken = loginResult.getAccessToken().getToken();
Log.i("accessToken",accessToken);
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("Test",response.toString());
Bundle bFacebookData = getFacebookData(object);
String name = bFacebookData.getString( "name");
String email = bFacebookData.getString( "email");
String gender = bFacebookData.getString( "gender");
String profilepic = bFacebookData.getString( "profile_pic");
String age_range = bFacebookData.getString( "age_range");
String birthday = bFacebookData.getString( "birthday");
String posts = bFacebookData.getString( "posts{message}");
Intent in = new Intent(SocialActivity.this, Facebook_Profile.class);
in.putExtra("name", name);
in.putExtra("email", email);
in.putExtra("gender", gender);
in.putExtra("Image", profilepic);
in.putExtra("posts", posts);
startActivity(in);
try{
File root = new File(Environment.getExternalStorageDirectory(),"Anique Text Files");
if (! root.exists()){
root.mkdir();
}
File filePath = new File(root, "myFile.txt");
FileWriter writer = new FileWriter(filePath);
writer.append("Name : "+ name+"\n");
writer.append("\n");
writer.append("Email : "+ email+"\n");
writer.append("\n");
writer.append("Gender : "+ gender+"\n");
writer.append("\n");
writer.append("Age : "+ age_range+"\n");
writer.append("\n");
writer.append("Birthday : "+ birthday+"\n");
writer.append("\n");
writer.append("Posts : "+ posts+"\n");
writer.append("\n");
writer.flush();
writer.close();
Toast.makeText(SocialActivity.this,"Success!! Logged into Anique_Profile as per given user information. ",Toast.LENGTH_LONG).show();
Toast.makeText(SocialActivity.this, "Data is Written!", Toast.LENGTH_LONG).show();
}catch (Exception ex){
ex.printStackTrace();
}
progressDialog.hide();
finish();
}
});
Bundle params = new Bundle();
params.putString("fields", "id, name, email, gender, age_range, birthday, posts, feed");
request.setParameters(params);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
}
private Bundle getFacebookData(JSONObject object) {
Bundle bundle = new Bundle();
try{
id = object.getString("id");
profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=200");
Log.i("profile_pic", profile_pic +"");
bundle.putString("profile_pic",profile_pic.toString());
}catch (JSONException ex){
ex.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
bundle.putString("idFacebook",id);
if(object.has("name")){
try{
bundle.putString("name", object.getString("name"));
}catch (JSONException ex){
ex.printStackTrace();
}
}
if(object.has("email")){
try{
bundle.putString("email", object.getString("email"));
}catch (JSONException ex){
ex.printStackTrace();
}
}
if(object.has("gender")){
try{
bundle.putString("gender", object.getString("gender"));
}catch (JSONException ex){
ex.printStackTrace();
}
}
if(object.has("age_range")){
try{
bundle.putString("age_range", object.getString("age_range"));
}catch (JSONException ex){
ex.printStackTrace();
}
}
if(object.has("birthday")){
try{
bundle.putString("birthday", object.getString("birthday"));
}catch (JSONException ex){
ex.printStackTrace();
}
}
if(object.has("posts")){
try{
bundle.putString("posts", object.getString("posts"));
}catch (JSONException ex){
ex.printStackTrace();
}
}
return bundle;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
答案 0 :(得分:0)
我希望获取当前用户个人资料状态,以便捕获特定关键字以预测某些内容。在我的应用程序上向用户显示帖子没有异议。但这似乎并不合适,因为此处涉及预测。
您需要对数据做一些事情,这些数据或多或少直接有益于用户的“应用内体验”开始......如果唯一的目的是对数据进行背景分析,Facebook将不会批准这一点。与用户体验没有直接联系。
阅读用户帖子所需的权限为user_posts
,但文档会将此列为不适用的用例之一:
不可见地使用此数据,例如情绪分析或防范垃圾邮件机器人。
https://developers.facebook.com/docs/facebook-login/permissions/v3.0#reference-user_posts