2011-03-02 194 views
4

有沒有人有過如何使用Android Graph API的例子?Android Facebook圖形API

我被基礎知識困住了,比如在Facebook上發貼文到牆上。

我正在使用適用於Android的Facebook SDK。以下是我迄今爲止:

Bundle params = new Bundle(); 
    params.putString("message", "picture caption"); 

    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); 
    mAsyncRunner.request("me/feed", params, "POST", new RequestListener() { 

     @Override 
     public void onMalformedURLException(MalformedURLException e, Object state) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onIOException(IOException e, Object state) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onFileNotFoundException(FileNotFoundException e, Object state) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onFacebookError(FacebookError e, Object state) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onComplete(String response, Object state) { 
      // TODO Auto-generated method stub 

     } 
    }, "foo"); 

什麼也沒有發生,我的logcat說:

03-02 22:10:02.973: WARN/Bundle(1930): Key message expected byte[] but value was a java.lang.String. The default value <null> was returned. 
    03-02 22:10:02.983: WARN/Bundle(1930): Attempt to cast generated internal exception: 
    03-02 22:10:02.983: WARN/Bundle(1930): java.lang.ClassCastException: java.lang.String 
    03-02 22:10:02.983: WARN/Bundle(1930):  at android.os.Bundle.getByteArray(Bundle.java:1305) 
    03-02 22:10:02.983: WARN/Bundle(1930):  at com.facebook.android.Util.encodePostBody(Util.java:63) 
    03-02 22:10:02.983: WARN/Bundle(1930):  at com.facebook.android.Util.openUrl(Util.java:182) 
    03-02 22:10:02.983: WARN/Bundle(1930):  at com.facebook.android.Facebook.request(Facebook.java:559) 
    03-02 22:10:02.983: WARN/Bundle(1930):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253) 
+0

給出從你的錯誤消息,它看起來u能提供鏈接就像你正在爲你的參數鍵入一個字符串Bundle當出於某種原因期望的字節數組。由於我沒有實現他們的API,我無法告訴你其他的事情。 – 2011-03-02 21:30:54

+0

是的。我也嘗試了一個字節數組,但它仍然抱怨。我希望有更多關於如何實現這個API的信息。唯一我遇到過的是PHP。 – whirlwin 2011-03-02 21:58:27

回答

2

試試這個

Bundle params = new Bundle(); 
params.putString(Facebook.TOKEN, token); 
params.putString("message", "graph api");  
mAsyncRunner.request("/me/feed", params,"POST", new SampleUploadListener(),null); 

您可以使用

 token = mFacebook.getAccessToken(); 
2

你可以做到這一點,如: - 捆綁消息 給予說明和如果u想分享從服務器的一些形象,我下面

Bundle params = new Bundle(); 
     params.putString("message", "description"); 
     params.putString("link","your image url"); 

new GraphRequest(AccessToken.getCurrentAccessToken(), 
      "me/feed", 
      params, 
      HttpMethod.POST, 
      new GraphRequest.Callback() { 
       @Override 
       public void onCompleted(GraphResponse response) { 
        Log.d(TAG, "" + response.toString()); 
        try { 
         JSONObject jsonObject = new JSONObject(response.getRawResponse()); 
         if (jsonObject != null) { 
          String postId = jsonObject.getString("id"); 
          if (postId != null && !postId.equalsIgnoreCase("")) { 
           hideProgressDialog(); 
           Log.d("postId", "" + postId); 
          } else { 
           hideProgressDialog(); 
           Utils.showToast(getActivity(), getResources().getString(R.string.txt_try_again)); 
          } 
         } else { 
          hideProgressDialog(); 
          Utils.showToast(getActivity(), getResources().getString(R.string.txt_try_again)); 
         } 

        } catch (JSONException e) { 
         hideProgressDialog(); 
         e.printStackTrace(); 
        } catch (Throwable e) { 
         hideProgressDialog(); 
         e.printStackTrace(); 
        } 

       } 
      }).executeAsync(); 
+0

工作很好的很好的解決方案。 – aj0822ArpitJoshi 2018-03-05 11:22:40