I am making an application in which I want to fetch the profile pictures of a facebook user. I tried to fetch the albums from facebook but I am getting null response from the facebook server.
I have tried it with multiple users but its not working.Can anyone help me?
here is my code:
mAsyncRunner.request(fbuserid+"/albums", new RequestListener()
{
#Override
public void onComplete(String response, Object state)
{
try
{
JSONObject albumjson = Util.parseJson(response);
JSONArray albums = albumjson.getJSONArray("data");
for (int i =0; i < albums.length(); i++)
{
JSONObject album = albums.getJSONObject(i);
String album_name = album.getString("name");
String album_id = album.getString("id");
System.out.println("albumname "+album_name +album_id);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state)
{
}
#Override
public void onFileNotFoundException(FileNotFoundException e,Object state)
{
}
#Override
public void onMalformedURLException(MalformedURLException e,Object state)
{
}
#Override
public void onFacebookError(FacebookError e, Object state)
{
}
});
You should try to get by below code:
If you have album id then
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{album-id}/photos",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
And If it's for a particular person then
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/photos",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
For this you need to set Permissions
1) Any valid access token if the photos are public.
2) A user access token user_photos permission to retrieve any photos that the session user has uploaded that are not public.
Reference : https://developers.facebook.com/docs/graph-api/reference/v2.0/album/photos
Hope it will help.
For detailed explanation please refer below link :
iOS : Facebook Album Photos Picker
Related
Currently i am using facebook sdk 4.4 and i am want to fetch all the facebook friend list who is not uses my app.
I have written below code:
GraphRequestBatch batch = new GraphRequestBatch(
GraphRequest.newMyFriendsRequest(token,
new GraphRequest.GraphJSONArrayCallback() {
#Override
public void onCompleted(JSONArray jsonArray,
GraphResponse response) {
try {
//my code
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}), GraphRequest.newMeRequest(token,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
System.out.println("meJSONObject: "
+ object);
System.out.println("meGraphResponse: "
+ response);
}
}));
batch.addCallback(new GraphRequestBatch.Callback() {
#Override
public void onBatchCompleted(GraphRequestBatch graphRequests) {
// Log.i(TAG, "onCompleted: graphRequests "+ graphRequests);
}
});
batch.executeAndWait();
From above code it is give my the list of friends who is using my app.
But i want the list of friends who is not using my app or not login through my app.
How can i do this?
As of the new graph, it is only possible to get friends who also uses the app
https://developers.facebook.com/docs/graph-api/reference/v2.4/user/friends
This is my login function:-
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,new String[] { "email", "publish_stream" },
new DialogListener() {
#Override
public void onCancel() {
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
This my function to get facebook likes and attributes:
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
id=profile.getString("id");
// getting name of the user
name = profile.getString("name");
// getting email of the user
email = profile.getString("email");
//about=profile.getString(about);
bio=profile.getString(bio);
birthday=profile.getString(birthday);
gender=profile.getString(gender);
link=profile.getString(link);
locale=profile.getString(locale);
political=profile.getString(political);
quotes=profile.getString(quotes);
relationship_status=profile.getString(relationship_status);
relegion=profile.getString(relegion);
website=profile.getString(website);
Log.d("Name",name);
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
//Getting Facebook Like
Session session = Session.getActiveSession();
new Request(
session,
"/{id}/likes",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.d("Facebook Likes", response.toString());
}
}
).executeAsync();
/*Session session = Session.getActiveSession();
Request.Callback callback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// response should have the likes
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
Log.d("Facebook Likes", response.toString());
}
};
Request request = new Request(session, "me/likes", null, HttpMethod.GET, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();*/
ERROR:-
D/Request﹕ Warning: Sessionless Request needs token but missing either application ID or client token.
I am getting other attributes as
28959-29486/com.example.sensetest D/Profile﹕ {"id":"************","email":"***********","first_name":"***","gender":"male","last_name":"****","link":"****","locale":"en_US","middle_name":"Singh","name":"Rituraj Singh Rathore","timezone":5.5,"updated_time":"2015-03-22T11:01:08+0000","verified":true}
The problem is I am not able to get facebook likes of user who has logged in. I have tried also How to get user likes from Facebook with Android sdk 3.0.
But I didn't got desired result.
Help me regarding this, Where am i doing wrong. What should i do to make it right?
Anybody has working sample code for Facebook login in Android using the code snippet mentioned in Facebook developer site ??I couldn't understand it properly.I want to get the name and profile picture of logged in user.I want my app to display the name and profile picture as long as the session is active and need to modify the details if user changed any.
Currently what i do is,saving access token and name in shared preferences on first login and saving the image in sd card and checking access token value during each app launch.If access token value is not null,then i display the name from shared preferences and profile picture from sd card.I know that this is not the right way to do this.Somebody please help me with this.
You can this https://github.com/sromku/android-simple-facebook library it is pretty well defined and can get details of methods by searching simle facebook android on google. Do whatever you want to do with facebook with this library....happy coding
Try this code
public class LoginActivity extends Activity {
private Button butLogin, butMaps, butJackpot, butAdministrator, buttonMenu;
public static String APP_ID = " paste your app_id";
public static Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
public static SharedPreferences mPrefs;
private static final String TAG = "Activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginToFacebook();
}
// Method to call the Facebook login
protected void loginToFacebook() {
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
mPrefs = getSharedPreferences("faceBook", MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email", "public_profile",
"publish_stream" }, Facebook.FORCE_DIALOG_AUTH,
new DialogListener() {
#Override
public void onCancel() {
}
#Override
public void onComplete(Bundle values) {
getProfileInformation();
}
#Override
public void onError(DialogError error) {
}
#Override
public void onFacebookError(FacebookError fberror) {
}
});
} else {
getProfileInformation();
}
}
// FaceBook getting profile information
public void getProfileInformation() {
showLoadingImage();
Helper.setFacebookLogin(getApplicationContext(), true);
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
String json = response;
try {
Log.i("JSOB", json);
JSONObject profile = new JSONObject(json);
try {
Bitmap bmp = null;
URL image_value = new URL("http://graph.facebook.com/"
+ profile.getString("id")
+ "/picture?type=large");
bmp = BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
profile_pic.setImageBitmap(bmp);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String first_name= profile.getString("first_name"));
String last_name=profile.getString("last_name"));
String email=profile.getString("email"));
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
// faceBook login method end
}
I use this code to get Facebook user informations. Most of informations are shown but the fact I need is user-birthday. There is no user-birthday in json array. The source code is here. Getting user information code is here
public void getProfileInformation() {
mAsyncRunner.request("me", new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Profile", response);
String json = response;
try {
// Facebook Profile JSON data
JSONObject profile = new JSONObject(json);
Log.i("profile",profile+"");
// getting name of the user
final String name = profile.getString("name");
// getting email of the user
final String email = profile.getString("email");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onIOException(IOException e, Object state) {
}
#Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
#Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
#Override
public void onFacebookError(FacebookError e, Object state) {
}
});
}
I need some advices. Please!!!
02-03 21:29:35.700: D/Profile(1350): {"id":"1550015884","name":"Aung Htoon Kyaw","first_name":"Aung","middle_name":"Htoon","last_name":"Kyaw","link":"https:\/\/www.facebook.com\/aung.htoonkyaw","hometown":{"id":"105988982772786","name":"Yangon, Burma"},"location":{"id":"115804185098639","name":"Rangoon, Yangon, Burma"},"work":[{"employer":{"id":"107622942602570","name":"KMMGroup"}}],"favorite_athletes":[{"id":"450149851675417","name":"Gohan Kazuto Uzumaki"},{"id":"339211762793623","name":"Natsu Dragneel"},{"id":"1347550405383513","name":"Instagram Girls"},{"id":"331817313532412","name":"Hinata Hyuga"},{"id":"481643341874600","name":"Erza Scarlet"},{"id":"123163411183595","name":"Sanji"}],"education":[{"school":{"id":"109294362421667","name":"BEST High School"},"year":{"id":"140617569303679","name":"2007"},"type":"High School"},{"school":{"id":"104056766297330","name":"University of Computer Studies, Yangon"},"year":{"id":"141778012509913","name":"2008"},"type":"College","with":[{"id":"1059125424","name":"Nara ChannLynn"}]},{"school":{"id":"106398082730475","name":"UCSY"},"year":{"id":"141778012509913","name":"2008"},"type":"College"}],"gender":"male","email":"waptan22\u0040gmail.com","timezone":6.5,"locale":"en_US","verified":true,"updated_time":"2013-09-12T14:32:54+0000","username":"aung.htoonkyaw"}
This is logcat output.
to get more personal details you need a user permission, so pragmatically you need to add permission to your session and prompt it to the user to accept/deny this permission.
if you are following the Androidhive tutorial, I am afraid it is the old Facebook SKD so you need to read the new version of the SDK, to solve your problem check this page out and specifically Step 2
on snippet of adding permission code:
authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "user_likes"));
...
I am working on an android app in which i need to login with Facebook and fetch facebook friends.
I am using below code for fetching facebook friends:
public class FriendsRequestListener implements RequestListener {
/**
* Called when the request to get friends has been completed.
* Retrieve and parse and display the JSON stream.
*/
public void onComplete(final String response) {
try {
// process the response here: executed in background thread
Log.d("Facebook-Example-Friends Request", "response.length(): " +
response.length());
Log.d("Facebook-Example-Friends Request", "Response: " + response);
final JSONObject json = new JSONObject(response);
JSONArray d = json.getJSONArray("data");
int l = (d != null ? d.length() : 0);
Log.d("Facebook-Example-Friends Request", "d.length(): " + l);
for (int i=0; i<l; i++) {
JSONObject o = d.getJSONObject(i);
String n = o.getString("name");
String id = o.getString("id");
}
// Only the original owner thread can touch its views
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
}
}
#Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
The way to call this class:
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/friends", new FriendsRequestListener());
The problem is that my code is not doing anything on call of above code.
you can use this api https://graph.facebook.com/v1.0/me/friends?fields=picture,id,username&limit=${limit}&access_token=${accessToken}
this is a v1.0 api which allows user to fetch all friends list
To get all your facebook friends list you need to get "taggable_friends"
GraphRequestAsyncTask r = GraphRequest.newGraphPathRequest(fbToken,
"/me/taggable_friends", new Callback() {
#Override
public void onCompleted(GraphResponse response) {
//here you can parse the response
}
}
fbToken is your token from login. However, facebook uses paging, so you need to get the next page and then next value from friends request so you can parse it.
response.getJSONObject("paging").getString("next");
to get next page users using StringRequest
StringRequest sr = new StringRequest(Request.Method.GET, next,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
JSONObject friends = null;
try {
friends = new JSONObject(response);
parseResponse(friends);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
Check the full code Facebook friends list