Reputation: 323
I'm trying to fetch page post from the facebook.fb is giving data of likes based on paging concept so able get 1000 likes of a post and other likes data is in the next link.My code is working fine to get the post data but when I wanted to save the all likes of the of post in single string obj only first 1000 likes are stored in the String obj other likes not stored I Tried out with appending also but json formate is changing.I'm not having any idea how to work out for storing the all the likes of posts in one single string object.
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);
}}
Upvotes: 0
Views: 596
Reputation: 323
Applied looping on every likes_json object and added to the main likes_Json object.
for (int i = 0; i < addlikes.length(); i++) //looped
{
JSONObject addslikobj = addlikes.getJSONObject(i); //every json object data is stored in addlikobj
likesdata.put(addslikobj);//finally kept all the addlikobjs of each loop is kept in likesdata Jsonobj
}
Upvotes: 0