从另一项活动在Android的墙上发布

时间:2014-11-10 10:16:07

标签: android facebook session facebook-graph-api post

我使用Facebook方法登录和在墙上发帖的Android活动。 我想在我登录后从其他活动中发布,所以每次会话时我都需要重新开启。 这是在Facebook墙上发布的方法:

public void postOnMyWall(String streamingLink, Context mycontext) {

    context = mycontext;  
    pref = context.getSharedPreferences("AppPref", Context.MODE_PRIVATE);
    String fbtoken = pref.getString("fbtoken", null);

    if(fbtoken != null) {

     if(!fbtoken.equals("")) {

         fbprogress = new ProgressDialog(context);

         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", "name");
            postParams.putString("caption", "my caption");
            postParams.putString("description", "my desc");
            postParams.putString("link", "my link");
            postParams.putString("picture", "https://linkto/my.png");

            Request.Callback callback= new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                            .getGraphObject()
                            .getInnerJSONObject();
                    String postId = null;

                    fbprogress.dismiss();

                    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();
                        fbprogress.dismiss();
                        Toast.makeText(context, "Errore during posting on Facebook", Toast.LENGTH_SHORT).show();

                    } else {

                         Toast.makeText(context, "posted on Facebook", Toast.LENGTH_SHORT).show();

                    }
                }
            };

            fbprogress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            fbprogress.setIndeterminate(true);
            fbprogress.show();

            Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);

            final RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();

            Handler fb_post_handler = new Handler();
            fb_post_handler.postDelayed(new Runnable()
            {
              @Override
              public void run() {
                  if ( task.getStatus() == AsyncTask.Status.RUNNING ) {
                      task.cancel(true);
                      Toast.makeText(SocialAccess.this, "Connection timed out", Toast.LENGTH_SHORT).show();
                      fbprogress.dismiss();


                  }
              }
            }, 20000 );


        } 


         else {
             Log.d("SOCIAL", "in posting wall, session is null");
         }





        } // end  if(!fbtoken.equals("") && fbtoken != null)

    } // end token != null


        else 
        {
            Log.d("SOCIAL","not logged in fb");
            fbloginrequested = true;

        }
      }

从其他活动中,我打电话:

SocialAccess socialogin = new SocialAccess(); socialogin.postOnMyWall(facebook_post_link,context);

如果我每次登录都不重新打开会话,那么会话似乎总是为空。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我知道这不是您具体问题的答案,但是,您尝试过Temboo吗?我甚至不记得上次我必须处理Facebook SDK实现时。在大多数情况下只是浪费时间。使用Temboo,您可以通过调用简单方法与所有社交网络进行交互。

答案 1 :(得分:1)

如果您已经在自己的应用中登录过一次,而且您的会话仍然是空的,那么您可以试试这个

 myFbSession = Session.getActiveSession();
 if(myFbSession!=null){
   // your code here
 }else{
         myFbSession = Session
                    .openActiveSessionFromCache(context);
         if(myFbSession!=null){
          // you can call your postOnWall method again here
          }
 }

如果上述解决方案不起作用,Github上有一个very simple library用于Facebook和Twitter共享,只能验证一次。