gpt4 book ai didi

android - 如何在 android 中使用 Graph API 在 facebook 上发布我的状态

转载 作者:太空狗 更新时间:2023-10-29 13:16:47 25 4
gpt4 key购买 nike

我想在我的 android 应用程序中使用 Graph API 来使用我的 android 应用程序发布文本状态,但不知道如何完美地使用 API...(我正在使用最新的 API)

这是我的登录 Activity

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.appevents.AppEventsLogger;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import java.util.Arrays;


public class LoginToFacebook extends AppCompatActivity {

public LoginButton loginButton;
private CallbackManager callbackManager;
public static final String LOGGED_IN_PREFFERENCE = "loggedInPrefference" ;
private AccessToken accessToken;
LoginResult loginResultOfClass;
static SharedPreferences mPrefs;

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if(mPrefs.getBoolean("loggedIn",false)==true);
{
Intent i = new Intent(LoginToFacebook.this,PostToWall.class);
startActivity(i);


}
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
mPrefs = getSharedPreferences(LOGGED_IN_PREFFERENCE, Context.MODE_PRIVATE);
callbackManager = CallbackManager.Factory.create();
accessToken = AccessToken.getCurrentAccessToken();

setContentView(R.layout.activity_login_to_facebook);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_friends"));


// Other app specific specialization




// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code

loginResultOfClass = loginResult;

SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("loggedIn",true);
editor.commit();


Intent i = new Intent(LoginToFacebook.this,PostToWall.class);
i.putExtra("UserName", loginResult.getAccessToken().getUserId());
i.putExtra("AuthToken", loginResult.getAccessToken().getToken());
startActivity(i);
}

@Override
public void onCancel() {
// App code

Toast.makeText(LoginToFacebook.this, "login Cancelled", Toast.LENGTH_SHORT).show();
}

@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(LoginToFacebook.this, "login exception"+exception.toString(), Toast.LENGTH_SHORT).show();

}
});




}





@Override
protected void onResume() {
super.onResume();
// Logs 'install' and 'app activate' App Events.
AppEventsLogger.activateApp(this);
}


@Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}


}

这是我想发布状态的 Activity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.widget.LoginButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Arrays;

public class PostToWall extends AppCompatActivity {
TextView info;
private LoginButton loginButton;
private EditText post_text;
Button post_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_post_to_wall);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
post_text = (EditText)findViewById(R.id.post_text);
post_button = (Button) findViewById(R.id.post_button);

loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setPublishPermissions(Arrays.asList("publish_actions"));


setSupportActionBar(toolbar);
info = (TextView) findViewById(R.id.info);
Intent i = getIntent();
info.setText(i.getStringExtra("UserName") + i.getStringExtra("AuthToken"));

post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


Bundle params = new Bundle();
params.putString("message", "hello post");
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("value", "SELF");
} catch (JSONException e) {
e.printStackTrace();
}
params.putString("privacy", jsonObject.toString());

/* make the API call */

GraphRequest request = new GraphRequest();
request.setAccessToken(AccessToken.getCurrentAccessToken());
request.setHttpMethod(HttpMethod.POST);

request.setParameters(params);

request.setGraphPath("/me/feed");
request.setCallback(new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {

Toast.makeText(PostToWall.this, "Post Success", Toast.LENGTH_SHORT).show();


}
});

request.executeAsync();



}
});

}


}

我不知道为什么 GraphRequest 对我不起作用...帮我找出我的错误...

最佳答案

请尝试以下代码:

Bundle postContents = new Bundle();
String selectedImageURL = galleryObj.getImageURL(mImageSelected);
mImageStory = mTextBox.getText().toString();

/*postContents.putString("attachment", "{\"name\":\""+mImageStory+"\","
+"\"media\":[{\"type\":\"image\",\"src\":\""+selectedImageURL+"\",\"href\":\""+selectedImageURL+"\"}]"
+"}");*/

//Below code is for posting text only
postContents.putString("attachment", "{\"name\":\""+mImageStory+"\""
+"}");

// OLD REST API stream.publish used to publish data onto the wall
mFacebook.dialog(mContext, "stream.publish", postContents, new AndroidDialogListener());

以下是用于在墙上发布带有文本的图像的监听器:

public class AndroidDialogListener extends BaseDialogListener {

public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);

// get the post id and send a request to get it posted on wall
// once posted/failure then call back invoked
mAsyncRunner.request(postId, new WallPostRequestListener());

} else {
Log.d("Facebook-Example", "No wall post made");
}
}
}

关于android - 如何在 android 中使用 Graph API 在 facebook 上发布我的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com