For getting FaceBook feed for the User,
we require the following items
SocialMediaFeedItem.java
Example Response :
we require the following items
- Facebook_APP_ID
- library project code for Facebook(Facebook SDK)
private static class FacebookReader {
//TODO:Change this APP ID with customer account
private final String Facebook_APP_ID = "Fill up App ID here";
private final String[] PERMISSIONS = new String[] { "read_stream",
"offline_access" };
private Facebook mFacebook;
private Activity mActivity;
private FeedListener mFeedListener;
public void getFeed(Activity activity,FeedListener feedListener){
this.mActivity = activity;
this.mFeedListener = feedListener;
this.mFacebook = new Facebook(Facebook_APP_ID);
SessionStore.restore(this.mFacebook, this.mActivity);
if (this.mFacebook.isSessionValid()) {
new GetFBFeed().execute();
} else {
this.mFacebook.authorize(activity, PERMISSIONS,
Facebook.FORCE_DIALOG_AUTH, new FbLoginDialogListener());
}
}
private class GetFBFeed extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... params) {
String response =null;
try {
response = mFacebook.request("99423386070/feed");
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String response) {
if(response != null){
ArrayList<SocialMediaFeedItem> facebook_feeds = parseJSONResponse(response);
if(facebook_feeds != null)
mFeedListener.onSuccess(facebook_feeds);
else
mFeedListener.onError("Error in parsing FaceBook JSON response");
}else{
mFeedListener.onError("Error in getting FaceBook Feed");
}
}
}
private ArrayList<SocialMediaFeedItem> parseJSONResponse(String jsonData) {
ArrayList<SocialMediaFeedItem> feeds = new ArrayList<SocialMediaFeedItem>();
try {
JSONObject jObj = new JSONObject(jsonData);
jObj.getJSONArray("data");
JSONArray jsonArray = jObj.getJSONArray("data");
String profile_image ="http://graph.facebook.com/99423386070/picture?type=small";
URL url = new URL(profile_image);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String heading = jsonObject.getJSONObject("from").getString("name");
String content = "";
if ( ! jsonObject.isNull("message"))
content += jsonObject.getString("message");
if ( ! jsonObject.isNull("description")) {
content = content.equalsIgnoreCase("") ? content : content
+ "\n\t";
content += jsonObject.getString("description");
}
if ( ! content.equalsIgnoreCase("")){
SocialMediaFeedItem feedItem = new SocialMediaFeedItem(heading,content,url,null,null);
feeds.add(feedItem);
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return feeds;
}
private final class FbLoginDialogListener implements Facebook.DialogListener {
public void onComplete(Bundle values) {
SessionStore.save(mFacebook, mActivity);
new GetFBFeed().execute();
}
public void onFacebookError(FacebookError error) {
Toast.makeText(mActivity, "Facebook connection failed",
Toast.LENGTH_SHORT).show();
}
public void onError(DialogError error) {
Toast.makeText(mActivity, "Facebook connection failed",
Toast.LENGTH_SHORT).show();
}
public void onCancel() {
}
}
}
SocialMediaFeedItem.java
public class SocialMediaFeedItem {
public SocialMediaFeedItem(String heading, String summary, URL imageURL, Date datePosted,
URL link) {
this.heading =heading;
this.summary =summary;
this.imageURL =imageURL;
this.datePosted = datePosted;
this.link =link;
}
public String getHeading() {
return heading;
}
public String getSummary() {
return summary;
}
public URL getImageURL() {
return imageURL;
}
public Date getDatePosted() {
return datePosted;
}
public URL getLink() { return link; }
private String heading;
private String summary;
private URL imageURL;
private Date datePosted;
private URL link;
}
Example Response :
where is the FeedListener class. Please post with complete example and if possible please provide source code.
ReplyDelete