检查Facebook发布来自Android应用成功(Check if Facebook post w

2019-10-21 05:48发布

我使用来自FacebookAndroid 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();
        }



      }

Answer 1:

所述onCompleted(Response response)从回调方法接收的一个实例Response类。 你可以使用这个对象来检查,如果该请求已成功处理。 甲全成HTTP请求的响应码应与2xx的(例如200行,201 CREATED等)开始。

response.getConnection().getResponseCode();

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

FacebookRequestError error = response.getError();


文章来源: Check if Facebook post was successful from Android application