String strFB_graph = "https://graph.facebook.com/";
String strAccess_key = "&access_token=<your_app_access_token>";
String strID = "<fb_group_id>";
String strQueryType = "/feed";
String strFields = "?fields=message,id,full_picture,updated_time,from,created_time,likes.limit(0).summary(total_count),comments.limit(0).summary(total_count)&limit=3";
String strURL = strFB_graph + strID + strQueryType + strFields + strAccess_key;
//Parse URL into HttpURLConnection in order to open the connection in order to get the JSON data
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//Set the request to GET or POST as per the requirements
conn.setRequestMethod("GET");
//Use the connect method to create the connection bridge
conn.connect();
//Get the response status of the Rest API
int responsecode = conn.getResponseCode();
System.out.println("Response code is: " +responsecode);
//Iterating condition to if response code is not 200 then throw a runtime exception
//else continue the actual process of getting the JSON data
if(responsecode != 200)
throw new RuntimeException("HttpResponseCode: " +responsecode);
else
{
//Scanner functionality will read the JSON data from the stream
Scanner sc = new Scanner(url.openStream());
while(sc.hasNext())
{
inline+=sc.nextLine();
}
System.out.println("\nJSON Response in String format");
System.out.println(inline);
//Close the stream when reading the data has been finished
sc.close();
}
//JSONParser reads the data from string object and break each data into key value pairs
JSONParser parse = new JSONParser();
//Type caste the parsed json data in json object
JSONObject jsonobjData = (JSONObject)parse.parse(inline);
//Store the JSON object in JSON array as objects (For level 1 array element i.e Results)
JSONArray jsonarrData = (JSONArray) jobj.get("data");
JSONObject jsonobjFeed;
JSONObject jsonobjFrom;
JSONObject jsonobjLikes;
JSONObject jsonobjLikesCount;
JSONObject jsonobjFromPic;
JSONObject jsonobjFromPicData;
String strArrFromID;
//Get data for Results array
for(int i=0;i<jsonarrData.size();i++)
{
//Store the JSON objects in an array
//Get the index of the JSON object and print the values as per the index
jsonobjFeed = (JSONObject)jsonarrData.get(i);
jsonobjFrom = (JSONObject)jsonobjFeed.get("from");
jsonobjLikes = (JSONObject)jsonobjFeed.get("likes");
jsonobjLikesCount = (JSONObject)jsonobjLikes.get("summary");
System.out.println("Feed item " + i);
System.out.println("\nPost : " +jsonobjFeed.get("message"));
System.out.println("\nAttachment: " + jsonobjFeed.get("full_picture"));
System.out.println("\nTime: " + jsonobjFeed.get("created_time"));
System.out.println("\nLikes Count: " + jsonobjLikesCount.get("total_count"));
// System.out.println("\nComments Count: " + jsonobjCommentsCount.get("total_count"));
System.out.println("\nFrom: " + jsonobjFrom.get("name"));
}
//Disconnect the HttpURLConnection stream
conn.disconnect();
package com.sample;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class WPFeed {
public static void main(String [] args)
{
//inline will store the JSON data streamed in string format
String inline = "";
String strFB_graph = "https://graph.facebook.com/";
String strAccess_key = "&access_token=<your_app_access_token>";
String strID = "<your_group_id>";
String strQueryType = "/feed";
String strFields = "?fields=message,id,full_picture,updated_time,from,created_time,likes.limit(0).summary(total_count),comments.limit(0).summary(total_count)&limit=3";
String strURL = strFB_graph + strID + strQueryType + strFields + strAccess_key;
System.out.println(strURL);
try
{
URL url = new URL(strURL);
//Parse URL into HttpURLConnection in order to open the connection in order to get the JSON data
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//Set the request to GET or POST as per the requirements
conn.setRequestMethod("GET");
//Use the connect method to create the connection bridge
conn.connect();
//Get the response status of the Rest API
int responsecode = conn.getResponseCode();
System.out.println("Response code is: " +responsecode);
Scanner sc;
//Iterating condition to if response code is not 200 then throw a runtime exception
//else continue the actual process of getting the JSON data
if(responsecode != 200)
throw new RuntimeException("HttpResponseCode: " +responsecode);
else
{
//Scanner functionality will read the JSON data from the stream
sc = new Scanner(url.openStream());
while(sc.hasNext())
{
inline+=sc.nextLine();
}
// System.out.println("\nJSON Response in String format");
// System.out.println(inline);
//Close the stream when reading the data has been finished
sc.close();
}
//JSONParser reads the data from string object and break each data into key value pairs
JSONParser parse = new JSONParser();
//Type caste the parsed json data in json object
// JSONObject jsonobjData = (JSONObject)parse.parse(inline);
JSONObject jsonobjData = (JSONObject)parse.parse(inline);
//Store the JSON object in JSON array as objects (For level 1 array element i.e Results)
JSONArray jsonarrData = (JSONArray) jsonobjData.get("data");
JSONObject jsonobjFeed;
JSONObject jsonobjFrom;
JSONObject jsonobjLikes;
JSONObject jsonobjLikesCount;
JSONObject jsonobjFromPic;
JSONObject jsonobjFromPicData;
String strArrFromID;
//Get data for Results array
for(int i=0;i<jsonarrData.size();i++)
{
//Store the JSON objects in an array
//Get the index of the JSON object and print the values as per the index
jsonobjFeed = (JSONObject)jsonarrData.get(i);
jsonobjFrom = (JSONObject)jsonobjFeed.get("from");
jsonobjLikes = (JSONObject)jsonobjFeed.get("likes");
jsonobjLikesCount = (JSONObject)jsonobjLikes.get("summary");
System.out.println("Feed item " + i);
System.out.println("\nPost : " +jsonobjFeed.get("message"));
System.out.println("\nAttachment: " + jsonobjFeed.get("full_picture"));
System.out.println("\nTime: " + jsonobjFeed.get("created_time"));
System.out.println("\nLikes Count: " + jsonobjLikesCount.get("total_count"));
// System.out.println("\nComments Count: " + jsonobjCommentsCount.get("total_count"));
System.out.println("\nFrom: " + jsonobjFrom.get("name"));
strArrFromID = jsonobjFrom.get("id").toString();
strFields = "?fields=picture";
strURL = strFB_graph + strArrFromID + strFields + strAccess_key;
url = new URL(strURL);
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
responsecode = conn.getResponseCode();
System.out.println("strURL is: " +strURL);
System.out.println("Response code is: " +responsecode);
if(responsecode != 200)
throw new RuntimeException("HttpResponseCode: " +responsecode);
else
{
//Scanner functionality will read the JSON data from the stream
sc = new Scanner(url.openStream());
inline = "";
while(sc.hasNext())
{
inline+=sc.nextLine();
}
System.out.println("\nJSON Response in String format");
System.out.println(inline);
//Close the stream when reading the data has been finished
sc.close();
}
jsonobjFromPic = (JSONObject)parse.parse(inline);
jsonobjFromPicData = (JSONObject)jsonobjFromPic.get("picture");
jsonobjFromPicData = (JSONObject)jsonobjFromPicData.get("data");
if(null!=jsonobjFromPicData.get("url"))
System.out.println("\nFromImageSrc: " + jsonobjFromPicData.get("url"));
}
//Disconnect the HttpURLConnection stream
conn.disconnect();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
5 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |