我正试图从我的Android应用程序发布图像到Facebook墙。我正在使用facebook sdk。
当我MainActivity
调用FBConnectionActivity
来获取连接和ID时,我得到一个空指针异常
我正在调用两种方法: -
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
setConnection();
getID();
}
}
在FBConnectionActivity
中,这两种方法定义为:
public void setConnection() {
mContext = this;
mFacebook = Utility.mFacebook;
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public void getID(TextView txtUserName, ProgressBar progbar) {
username = txtUserName;
pb = progbar;
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
public void getID(){
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
在对isSession()
的调用中,对isSessionValid()
方法的方法调用始终返回false。
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
Log.e("isSession() method ", "acess_token : " + access_token );
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
public boolean isSessionValid() {
return (getAccessToken() != null) &&
((getAccessExpires() == 0) ||
(System.currentTimeMillis() < getAccessExpires()));
}
如果我的会话无效,我如何获得新的有效访问令牌。每次我尝试从我的应用程序上传图像时,方法isSessionValid()
都会返回false。我也收到以下错误: -
01-18 12:46:35.928: D/Facebook-Example(28459): Response: {"error_code":101,"error_msg":"Invalid API key","request_args":[{"key":"method","value":"photos.upload"},{"key":"format","value":"json"}]}
01-18 12:46:35.948: W/Facebook-Example(28459): Facebook Error: Invalid API key
请帮帮我。
我使用以下方法在Facebook上发布图片。这里有什么不对吗?
public void postImageonWall() {
Log.e("BrowsePictureActivity ", "Inside postImageonWall method");
byte[] data = null;
Bitmap bi = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
Log.d("BrowsePictureActivity ", "data.length : " + data.length);
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(),
null);
}
答案 0 :(得分:0)
您尚未发布包含将图片发布到Facebook墙的代码段
If you map wrong Key and Value Pair you will get Invalid API Key.
因此,请确保为Post图像使用正确的Key和Value Pair,并在添加附件和图像时小心。
尝试将图片发布到Facebook墙:
private void postToWall(String accessToken) {
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream); // bm is bitmap object of your image
byte[] byteArray = stream.toByteArray();
Bundle param = new Bundle();
param = new Bundle();
param.putString("message", "All");
param.putString("filename", "Test");
param.putByteArray("image", byteArray);
mAsyncRunner.request("me/photos", param, "POST", new fbRequestListener(), null);
}
public class fbRequestListener implements RequestListener {
@Override
public void onComplete(String response, Object state) {
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
}