我在墙上使用 Facebook Android Sdk 中的发布代码。
我想得到一些反馈,知道帖子是否成功。
如何检查?
RequestAsyncTask对象的任何方法?
非常感谢。
public void postOnMyWall(Context mycontext) {
context = mycontext;
pref = context.getSharedPreferences("AppPref", Context.MODE_PRIVATE);
String fbtoken = pref.getString("fbtoken", null);
if(!fbtoken.equals("") && fbtoken != null) {
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", "wef");
postParams.putString("caption", "few");
postParams.putString("description", "x");
postParams.putString("link", "https://mylink.some");
postParams.putString("picture", "https://mylink.some/img.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
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();
} else {
/*
Toast.makeText(activity
.getApplicationContext(),
"POSTED: " + postId,
Toast.LENGTH_LONG).show();
*/
}
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Toast.makeText(context, "posted on Facebook", Toast.LENGTH_SHORT).show();
}
}
else
{
Log.d("SOCIAL","not logged in fb");
Toast.makeText(context, R.string.facebook_login_request, Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
回调中的onCompleted(Response response)
方法接收Response
类的实例。您可以使用此对象检查请求是否已成功处理。成功的HTTP请求的响应代码应以2xx开头(例如200 OK,201 CREATED等)。
response.getConnection().getResponseCode();
如果由于某种原因失败,您可以检索详细错误,就像您在此行中所做的那样:
FacebookRequestError error = response.getError();