【问题标题】:Check if Facebook post was successful from Android application从 Android 应用程序检查 Facebook 帖子是否成功
【发布时间】:2015-01-02 19:22:57
【问题描述】:

我正在使用 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();
        }



      }

【问题讨论】:

    标签: android facebook api post sdk


    【解决方案1】:

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

    response.getConnection().getResponseCode();
    

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

    FacebookRequestError error = response.getError();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多