2016-07-20 3 views
0

Android로 Facebook의 Graph API에서 뉴스 피드를 얻으려고합니다. 이 JSON을 구문 분석하려고 할 때 NULL이 아니라 아무것도 가져 오지 않습니다.Facebook API 뉴스 피드 Android

이것은 내 피드이며 피드입니다. 이 코드는 50/50에서 stackoverflow 및 그래프 API 문서에서 가져온 것입니다.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_news_feed); 
    userProfile = (Profile) getIntent().getParcelableExtra("Profile"); 
    stringsForList = new ArrayList<>(); 
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, stringsForList); 
    newsFeed = (ListView)findViewById(R.id.newsFeed); 

    new GraphRequest(
      AccessToken.getCurrentAccessToken(), 
      "/me", 
      null, 
      HttpMethod.GET, 
      new GraphRequest.Callback() { 
       public void onCompleted(GraphResponse response) { 

        try { 
         JSONObject mainObject = response.getJSONObject(); 
         JSONObject feed = mainObject.getJSONObject("feed"); 
         JSONArray data = feed.getJSONArray("data"); 
         if(data == null) Log.d(LOG_TAG, "data is null"); 
         // StringBuilder stringForArray; 

         for(int i = 0; i < data.length(); i++) 
         { 
          //stringForArray = null; 
          JSONObject singleNews = data.getJSONObject(i); 
          if(singleNews == null) Log.d(LOG_TAG, "news is null"); 



         } 
        }catch(JSONException ex) 
        { 
         ex.printStackTrace(); 
        } 


       } 
      } 
    ).executeAsync(); 

} 

그리고 여기 JSON 파일을 구문 분석하려고합니다.

"feed": { 
"data": [ 
    { 
    "message": "Я їду на фестиваль ЗАХІД", 
    "story": "Oleg Misko shared Zaxidfest's photo — with Василь Угринюк and 2 others.", 
    "created_time": "2016-06-20T06:55:32+0000", 
    "id": "1272345062793233_1291027040925035" 
    }, 
    { 
    "story": "Oleg Misko shared Zaxidfest's post.", 
    "created_time": "2016-06-20T06:55:01+0000", 
    "id": "1272345062793233_1291026900925049" 
    }, 
    { 
    "message": "Я їду на фестиваль ЗАХІД", 
    "story": "Demian Mysakovets shared Zaxidfest's photo — with Sophia Hoshko and 2 others.", 
    "created_time": "2016-06-18T15:27:35+0000", 
    "id": "1272345062793233_1289904527703953" 
    }, 
    { 
    "story": "Oleg Misko shared Територія твого розвитку's post.", 
    "created_time": "2016-06-18T08:55:45+0000", 
    "id": "1272345062793233_1289698067724599" 
    }, 
    { 
    "story": "Oleg Misko shared JavaRush.RU's photo.", 
    "created_time": "2016-06-07T19:58:03+0000", 
    "id": "1272345062793233_1282518005109272" 
    }, 
    { 
    "story": "Oleg Misko shared a link.", 
    "created_time": "2016-03-31T15:42:38+0000", 
    "id": "1272345062793233_1234673696560370" 
    }, 
    { 
    "story": "Oleg Misko updated his profile picture.", 
    "created_time": "2016-03-19T09:53:02+0000", 
    "id": "1272345062793233_1220982634596143" 
    }, 
    { 
    "message": "posdravliayu.", 
    "created_time": "2016-02-19T15:44:14+0000", 
    "id": "1272345062793233_1200638139963926" 
    }, 
    { 
    "story": "Oleg Misko shared Zaxidfest's video.", 
    "created_time": "2016-01-25T09:59:35+0000", 
    "id": "1272345062793233_1184872831540457" 
    }, 
    { 
    "story": "Oleg Misko shared Територія твого розвитку's photo.", 
    "created_time": "2016-01-14T12:35:45+0000", 
    "id": "1272345062793233_1178560875504986" 
    } 
] 
+0

배열 데이터를 가져온 후 json 데이터를 가져 오는 방법은 무엇입니까? – Drv

+0

null이 표시되지 않지만이 JSON 파일에서 데이터를 올바르게 가져 오는 방법을 알지 못합니다. "story"와 "created_time"문자열을 가져와야합니다. –

답변

0

Drv, 답장을 보내 주셔서 감사합니다. 문제는이 뉴스 배열을 내 프로그램에 전달하는 방법을 알 수 없다는 것입니다. 그것은 단지 나에게 아무것도 돌려주지 않는다. 나는이

Bundle params = new Bundle(); 
    params.putString("fields", "id,email,gender,name"); 
    new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET, 
      new GraphRequest.Callback() { 
       @Override 
       public void onCompleted(GraphResponse response) { 
        if (response != null) { 
         try { 
          JSONObject data = response.getJSONObject(); 
          if (data.has("name")) { 
           nameText.setText(data.getString("name")); 
          } 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      }).executeAsync(); 

뭔가를 할 경우

그것은 성공적으로 계정의 사용자 이름을 날 다시 제공합니다. 뉴스 피드를 전달하려면 어떻게해야합니까? :