我想发布链接,如果用户登录facebook,如果没有让用户登录然后发布链接,但它在publishlink()方法中的jsonobject graphresponse显示NullPointerException
。
以下是我的代码..
public static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
public static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
public boolean pendingPublishReauthorization = false;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
public void ShareOnFacebook(String sharingpath)
{
Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
Session session = new Session.Builder(getBaseContext()).build();
Session.setActiveSession(session);
currentSession = session;
}
else if (!currentSession.isOpened()) {
// Ask for username and password
OpenRequest op = new Session.OpenRequest(this);
op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
op.setCallback(statusCallback);
List<String> permissions = new ArrayList<String>();
permissions.add("publish_stream");
op.setPermissions(permissions);
Session session = new Session(this);
Session.setActiveSession(session);
Log.d("session", "opening sesion for publish action!!");
session.openForPublish(op);
}
}
private class SessionStatusCallback implements Session.StatusCallback
{
@Override
public void call(final Session session, SessionState state, Exception exception)
{
//we have callback here
if (session.isOpened())
{
Log.d("session", "session is already opened ,we are in the callback section...going to publish link!!");
PublishLink();
}
}
}
public void PublishLink()
{
SharedPreferences prefs = getSharedPreferences("shared",MODE_PRIVATE);
String path = prefs.getString("path", "");
Session session = Session.getActiveSession();
// 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);
}
Bundle postParams = new Bundle();
postParams.putString("link", path);
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
Log.d("graphresponse", response.toString());
JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.d(
"JSON error "+ e.getMessage(), postId);
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(
getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(
getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
public boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
Log.d("data", "data coming to onactivityresult: "+data);
}
答案 0 :(得分:0)
这里的问题是requestNewPublishPermissions()方法是一个异步请求,当你调用它时,它会立即返回,但此时你还没有发布权限。
您正在做的是发出“发布”请求,无论您是否具有发布权限,因此响应将产生错误而不是GraphObject结果。
如果检查response.getError(),您可能会看到权限错误(在访问Response中的任何其他内容之前,您应该首先检查该权限)。
查看HelloFacebook示例,了解如何临时缓存您要发布的信息(等待requestNewPublishPermissions完成时),并仅在您拥有所有必需权限时发布。
答案 1 :(得分:0)
终于弄清楚
中有错误 Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
所以将其改为
Request request = Request.newStatusUpdateRequest(session, path, callback);
希望这可以帮助有这个问题的人。 所以我们不再需要创建新的Bundle .... 我们可以在上面的代码中删除create Bundle和postparams.putString行。