2013-03-19 143 views
0

好的,所以我想在用戶的牆上創建一個新帖子。我一直其次developers.facebook在公佈故事的方法我添加日誌,看看程序有通過代碼行提供了這個鏈接https://developers.facebook.com/docs/howtos/androidsdk/3.0/publish-to-feed/facebook發佈Feed在獲得發佈許可後卡在黑屏上

的代碼:

private void publishStory() { 
Session session = Session.getActiveSession(); 

if (session != null){ 

    Log.d("INFO","session is not NULL"); 

    // Check for publish permissions  
    List<String> permissions = session.getPermissions(); 
    if (!isSubsetOf(PERMISSIONS, permissions)) { 

     Log.d("INFO","setting permission"); 

     pendingPublishReauthorization = true; 
     Session.NewPermissionsRequest newPermissionsRequest = new Session 
       .NewPermissionsRequest(this, PERMISSIONS); 
    session.requestNewPublishPermissions(newPermissionsRequest); 
     return; 
    } 

    Bundle postParams = new Bundle(); 
    postParams.putString("name", "Facebook SDK for Android"); 
    postParams.putString("caption", "Build great social apps and get more installs."); 
    postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps."); 
    postParams.putString("link", "https://developers.facebook.com/android"); 
    postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png"); 

    Log.d("INFO","finish construct message"); 

    Request.Callback callback= new Request.Callback() { 
     public void onCompleted(Response response) { 

      Log.d("INFO","on request complete!"); 

      JSONObject graphResponse = response 
             .getGraphObject() 
             .getInnerJSONObject(); 
      String postId = null; 
      try { 
       postId = graphResponse.getString("id"); 
      } catch (JSONException e) { 
       Log.i(TAG, 
        "JSON error "+ e.getMessage()); 
      } 
      FacebookRequestError error = response.getError(); 
      if (error != null) { 
       Toast.makeText(getActivity() 
        .getApplicationContext(), 
        error.getErrorMessage(), 
        Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(getActivity() 
         .getApplicationContext(), 
         postId, 
         Toast.LENGTH_LONG).show(); 
      } 
     } 
    }; 

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

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

}

問題是,當我執行上面的方法時,程序運行良好,直到它彈出一個認證發佈權限窗口,我允許它,屏幕繼續再次到一個黑色的屏幕上有不確定的進度條,然後窗口只是解僱本身。

事實上,從logcat我可以得出結論,它不會通過「按要求完成」行。

爲什麼會發生這種情況?任何線索?

謝謝!

+0

是活動的這裏面的代碼,你重寫該活動的onActivityResult方法? – 2013-03-19 15:58:44

+0

是的,它在我的Activity中,我不重寫onActivityResult onActivityResult怎麼辦?對不起,我不知道這裏... – user724861 2013-03-20 02:25:25

+1

你需要重寫onActivityResult並調用Session.getActiveSession()。onActivityResult。否則,Session對象將永遠不會知道重新授權的結果。 – 2013-03-20 17:47:30

回答

2

噢,我目前採用該解決方案Updated - Android Facebook api 3.0 error: Cannot call LoginActivity with a null calling package

像你說的,我已經添加了onActivityResult這樣

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
} 

和它的作品!雖然我排列了對話框類

public void createFacebookConnection() { 
     Session session = new Session(MapScreen.this); 
     Session.setActiveSession(session); 

     Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); 

     Session.StatusCallback statusCallback = new Session.StatusCallback() { 
      @Override 
      public void call(Session session, SessionState state, Exception exception) { 
       String message = "Facebook session status changed - " + session.getState() + " - Exception: " + exception; 
       Log.w("Facebook test", message); 

       if (session.isOpened() || session.getPermissions().contains("publish_actions")) { 
        publishToWall(); 
       } else if (session.isOpened()) { 
        OpenRequest open = new OpenRequest(MapScreen.this).setCallback(this); 
        List<String> permission = new ArrayList<String>(); 
        permission.add("publish_actions"); 
        open.setPermissions(permission); 
        Log.w("Facebook test", "Open for publish"); 
        session.openForPublish(open); 
       } 
      } 
     }; 

     if (!session.isOpened() && !session.isClosed() && session.getState() != SessionState.OPENING) { 
      session.openForRead(new Session.OpenRequest(MapScreen.this).setCallback(statusCallback)); 
     } else { 
      Log.w("Facebook test", "Open active session"); 
      Session.openActiveSession(MapScreen.this, true, statusCallback); 
     } 
    } 

    void publishToWall() { 
     Session session = Session.getActiveSession(); 

     Bundle postParams = new Bundle(); 
     postParams.putString("name", "Merauke Android"); 
     postParams.putString("caption", hotelName); 
     postParams.putString("description", hotelName+" is a luxury hotel which is located in the city of Bandung."); 
     postParams.putString("link", "https://developers.facebook.com/android"); 
     postParams.putString("picture", "http://www.jejaringhotel.com/android/hotelimages/hotel-3.jpg"); 
     Log.i("DIALOG","feed set"); 

     Request.Callback callback = new Request.Callback() { 
      public void onCompleted(Response response) { 
       FacebookRequestError error = response.getError(); 
       if (error != null) { 
        Toast.makeText(MapScreen.this, error.getErrorMessage(), Toast.LENGTH_SHORT).show(); 
       } else { 
        JSONObject graphResponse = response.getGraphObject().getInnerJSONObject(); 
        String postId = null; 
        try { 
         postId = graphResponse.getString("id"); 
        } catch (JSONException e) { 
         Log.i("Facebook error", "JSON error " + e.getMessage()); 
        } 
        //TODO Toast.makeText(context, postId, Toast.LENGTH_LONG).show(); 
        Toast.makeText(MapScreen.this, "Posted on wall success!", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }; 

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

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

非常感謝您的建議Ming Li!謝謝!