2014-11-06 39 views
1

我使用的代碼從Facebook Android的Sdk在牆上。 我想獲得一些反饋,以瞭解帖子是否成功。 如何檢查?
來自RequestAsyncTask對象的任何方法? 非常感謝。檢查Facebook發佈是否成功從Android應用程序

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

從回調的onCompleted(Response response)方法接收Response類的一個實例。您可以使用此對象來檢查請求是否已成功處理。一個成功的HTTP請求的響應代碼應該以2xx開頭(例如200 OK,201 CREATED等)。

response.getConnection().getResponseCode(); 

如果由於某種原因失敗,你可以檢索詳細的錯誤,就像你已經在這行做的事:

FacebookRequestError error = response.getError(); 
相關問題