2016-07-20 127 views
0

我试图从Android的Facebook的Graph API获取新闻Feed。 当我试图解析这个JSON时,它没有得到任何东西,不是NULL,但没有。Facebook API新闻提要Android

这是我的代码,它得到的饲料。代码从计算器和图形API文档中获取50/50

@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数据的?或者你是否获得数据null? – Drv

+0

我没有变为null,但是我不知道如何从这个JSON文件中正确获取数据 需要采取“故事”和“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(); 

它给我回帐户的用户名成功。我怎样才能通过新闻提要? :(