2016-05-27 13 views
3

私はFacebookにメッセージを投稿し、投稿のidを取得したいと思います。 以下は私のコードです。GraphResponseからjsonへの応答

post.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Bundle params = new Bundle(); 
      params.putString("message", "This is a test message"); 
      /* make the API call */ 
      new GraphRequest(
       AccessToken.getCurrentAccessToken(), 
       "/me/feed", 
       params, 
       HttpMethod.POST, 
       new GraphRequest.Callback() { 
        public void onCompleted(GraphResponse response) { 

         /* handle the result */ 


        } 
       } 
      ).executeAsync();    
     } 
    }); 

と私はそのような応答を得る:

{ 
    Response: { responseCode: 200 }, 
    graphObject: {"id":"173048733066533_234919673546105"}, 
    error: null 
} 

私はgraphObject応答から、この "ID" が必要。あなたはこのコードを試すことができます

答えて

0

公式Facebookの文書を見here

あなたは非同期(Request.executeAsync())要求を行う場合は、onCompleted(レスポンスレスポンス)方法でRequest.Callbackを使用します。あなたがシリーズでリクエストを行う場合(あなたがメインスレッドにいないことを確認してください)、レスポンスはRequest.executeAndWait()メソッドによって返された値になります。 。私はそれが動作を期待

JSONObject graphResponse = response.getGraphObject().getInnerJSONObject(); 
        String postId = null; 
        try { 
         postId = graphResponse.getString("id"); 
        } catch (JSONException e) { 
         Log.i("Facebook error", "JSON error " + e.getMessage()); 
        } 

:Responseオブジェクトから

は、あなたが response.getGraphObject()getInnerJsonObject()

例を呼び出すことができます。

+1

ありがとう.. response.getGraphObject()。getInnerJsonObject()it's works –

0

try { 
    JSONObject jsonObject=new JSONObject(response.getRawResponse()); 
    JSONArray results = jsonObject.getJSONArray("graphObject"); 
    JSONObject first = results.getJSONObject(0); 
    JSONObject shipper = first.getJSONObject("id"); 
    String id = shipper.getString("id"); 
    Log.d("id",id); 

}catch (JSONException e){ 

} 

あなたがテストすることができますが、テストしたわけではありません。

関連する問題