Check if Facebook post was successful from Android application

落花浮王杯 提交于 2019-12-11 09:35:43

问题


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



      }

回答1:


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


来源:https://stackoverflow.com/questions/26776896/check-if-facebook-post-was-successful-from-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!