【发布时间】:2014-11-10 18:30:59
【问题描述】:
我有一个使用 Facebook 方法登录和在墙上发帖的 Android 活动。 我想在登录后从其他活动中发布,因此每次会话时我都需要重新打开。 这是在 Facebook 墙上发帖的方法:
public void postOnMyWall(String streamingLink, Context mycontext) {
context = mycontext;
pref = context.getSharedPreferences("AppPref", Context.MODE_PRIVATE);
String fbtoken = pref.getString("fbtoken", null);
if(fbtoken != null) {
if(!fbtoken.equals("")) {
fbprogress = new ProgressDialog(context);
Session session = Session.getActiveSession();
if (session != null){
Log.d("SOCIAL", "in posting wall, session is not null");
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
//pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "name");
postParams.putString("caption", "my caption");
postParams.putString("description", "my desc");
postParams.putString("link", "my link");
postParams.putString("picture", "https://linkto/my.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
fbprogress.dismiss();
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
/*
Log.i(activity.toString(),
"JSON error "+ e.getMessage());
*/
}
FacebookRequestError error = response.getError();
if (error != null) {
//Toast.makeText(activity
// .getApplicationContext(),
// "ERROR: " + error.getErrorMessage(),
// Toast.LENGTH_SHORT).show();
fbprogress.dismiss();
Toast.makeText(context, "Errore during posting on Facebook", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "posted on Facebook", Toast.LENGTH_SHORT).show();
}
}
};
fbprogress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
fbprogress.setIndeterminate(true);
fbprogress.show();
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
final RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Handler fb_post_handler = new Handler();
fb_post_handler.postDelayed(new Runnable()
{
@Override
public void run() {
if ( task.getStatus() == AsyncTask.Status.RUNNING ) {
task.cancel(true);
Toast.makeText(SocialAccess.this, "Connection timed out", Toast.LENGTH_SHORT).show();
fbprogress.dismiss();
}
}
}, 20000 );
}
else {
Log.d("SOCIAL", "in posting wall, session is null");
}
} // end if(!fbtoken.equals("") && fbtoken != null)
} // end token != null
else
{
Log.d("SOCIAL","not logged in fb");
fbloginrequested = true;
}
}
来自其他活动,我正在打电话:
SocialAccess socialogin = new SocialAccess(); socialogin.postOnMyWall(facebook_post_link, context);
如果我不每次登录都重新打开会话,会话似乎总是为空。
有什么建议吗?
【问题讨论】:
标签: android facebook session facebook-graph-api post