从Android应用程序检查Facebook发布是否成功

问题描述:

我正在使用 Facebook Android Sdk 中的这段代码在墙上进行发布. 我想获得一些反馈,以了解该帖子是否成功. 如何检查?
从RequestAsyncTask对象的任何方法? 非常感谢.

I'm using this code from the Facebook Android Sdk to post on wall. I would like to have some feedback to know if the post was successfull or not. How to check this ?
Any method from RequestAsyncTask object ? Many thanks.

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();
        }



      }

回调中的onCompleted(Response response)方法接收Response类的实例.您可以使用此对象检查请求是否已成功处理.成功的HTTP请求的响应代码应以2xx开头(例如200 OK,201 CREATED等).

The onCompleted(Response response) method from the callback receives an instance of the Response class. You can use this object to check if the request has been processed successfully. A successfull HTTP request's response code should begin with 2xx (e.g. 200 OK, 201 CREATED etc).

response.getConnection().getResponseCode();

如果由于某种原因失败,您可以检索详细的错误,就像您已经在此行中所做的一样:

If it failed for some reason, you can retrieve a detailed error, like you already do in this line:

FacebookRequestError error = response.getError();