如何打破分页并将数据保存在单个字符串对象中?

时间:2016-02-09 04:27:05

标签: java json facebook

enter image description here

我试图从facebook上获取页面帖子.fb正在根据分页概念提供喜欢的数据,因此能够获得1000个喜欢的帖子和其他喜欢的数据在下一个链接中。我的代码工作正常获取帖子数据,但是当我想在单个字符串obj中保存所有喜欢的帖子时,只有前1000个喜欢存储在String obj中其他喜欢没有存储我尝试追加,但json formate正在改变。我&# 39;我不知道如何在一个单独的字符串对象中存储所有类似的帖子。

package facebook;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.RawAPIResponse;
import facebook4j.auth.AccessToken;
import facebook4j.internal.org.json.JSONArray;
import facebook4j.internal.org.json.JSONException;
import facebook4j.internal.org.json.JSONObject;

public class Facebookthe{


public static void main(String[] args) throws FacebookException,JSONException, IOException {

// Generate facebook instance.
Facebook facebook = new FacebookFactory().getInstance();
// Use default values for oauth app id.
facebook.setOAuthAppId("XXXXXXXXXXX", "XXXXXXXXXXXXXX");
AccessToken accessTokenString = facebook.getOAuthAppAccessToken();
            String m ="AnushkaShetty/?fields=posts.limit(1).since(2015).until(now){id,message,name,type,picture,link,caption,description,icon,application,shares,updated_time,source,comments.limit(500).summary(true){comment_count,message,can_remove,id,created_time,can_like,like_count,comments{comment_count,comments{comment_count}}},place,object_id,privacy,status_type,created_time,story,parent_id,story_tags,full_picture,likes.limit(9999).summary(true){id,name,username}},id,hometown,website,about,location,birthday,name,tagged{message_tags},category,category_list,talking_about_count,likes";
RawAPIResponse res1 = facebook.callGetAPI(m);
 JSONObject jsonObject55= res1.asJSONObject();
System.out.println(jsonObject55); //609425942504811

        //z=sb.append(jsonObject55);

        JSONObject posts = jsonObject55.getJSONObject("posts");

        JSONArray data = posts.getJSONArray("data");

        JSONObject number = data.getJSONObject(0);

        JSONObject likes = number.getJSONObject("likes");

        JSONObject paging = likes.getJSONObject("paging");

        String s = paging.getString("next");

        int count =1;

        while(s != null)
        {
            count++;
            System.out.println("go to this link");

            URL oracle = new URL(s);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                                        yc.getInputStream()));
            String inputLine;
          /*  String oldfile="";
            String newfile="";*/

            JSONObject obj = new JSONObject();

            while ((inputLine = in.readLine()) != null) 
            {
                obj = new JSONObject(inputLine);                  
            }

          //  z+=sb.append(obj);
         //System.out.println(obj);
            //System.out.println(count);

            JSONObject jo = obj.getJSONObject("paging");

           try
           {
               s = jo.getString("next");
           }
           catch(Exception e)
           {
               s=null;
               System.out.println("there is no next");
           }

            in.close();

        }
        System.out.println(count);

}}

1 个答案:

答案 0 :(得分:0)

在每个likes_json对象上应用循环并添加到主要的likes_Json对象。

Form