How to query specific data from Facebook on Android integration? - android

I have integrated Facebook to my Android application.Now I want to fetch the information like specific posts which are only made by my application.
How can I do this ? Please help.
Edited.
In my application a user can post message to his wall.What I want is if user's friends also uses same application and post messages through the application.
So I want to retrieve all those messages of user from his wall which are made through the application(By him and his friends).

OK I have solved problem my own.
I have used fql for retrieving information I needed.
This is how I have used fql.
For retrieving messages & other data from my wall (only those which are made through my application).
private String getWallInfo()
{
try
{
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", "SELECT post_id, actor_id, app_id, message FROM stream where source_id = me() and app_id = "
+ ConnectData.APP_ID);
String response = facebook.request(params);
Log.i(TAG, "response = " + response);
return response;
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
For retrieving messages & other data from my friends wall (only those which are made through my application).
private String getFriendsWallInfo()
{
try
{
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query",
"SELECT post_id, actor_id, app_id, message FROM stream WHERE source_id IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND app_id="
+ ConnectData.APP_ID);
String response = facebook.request(params);
Log.i(TAG, "response = " + response);
return response;
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

Related

Wall post not display on Facebook wall from android application

it might be duplicate question so sorry please consider it as my problem .
i am facing problem image posting on facebook wall from android application but image not display on wall it is visible inside Photos-----> your photos section but i want this images should display on user time line or wall please help me .
my code is
Bundle params = new Bundle();
// Inflate Edit text for Facebook Message
params.putString("method", "photos.upload");
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("app_id", mAPP_ID); // I've tried with/without this,
// same result
params.putString("message", message);
Bitmap b = BitmapFactory
.decodeResource(getResources(), R.drawable.test);
Log.i("Bitmap Image is here", "Bitmap" + b);
if (b != null) {
Log.i("Bitmap", "value");
} else {
Log.i("Bitmap", "null");
}
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
if (data != null) {
params.putByteArray("picture", data);
Log.i("final", "value" + message);
params.putString("access_token", facebook.getAccessToken());
try {
facebook.request("me");
facebook.request("me/photos", params, "POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I faced similar issue some time ago: FB combines all photo uploads into single post on your timeline when you upload more than one photo during a short period of time. Also, it looks like you do not provide custom photo message and privacy settings, so your code can be significantly simplified (I assume you are using the latest FB android sdk, have the session opened, and already requested publish_stream permission):
Bitmap image2Upload = ...;
Request uploadRequest = Request.newUploadPhotoRequest(Session.getActiveSession(), image2Upload, null);
String photoId = null;
String postId = null;
try {
Response uploadResponse = Request.executeAndWait(uploadRequest);
JSONObject json = uploadResponse.getGraphObject().getInnerJSONObject();
photoId = json.getString("id");
postId = json.optString("post_id");
} catch(Throwable t) {
Log.e(TAG, "Photo upload request failed!", t);
}

How to post on Facebook page's wall in Android?

Hi I want to post any message or link to an Facebook page.What I have tried is this.
void postInfo()
{
try
{
Bundle parameters = new Bundle();
parameters.putString("message", messageFacebook);
parameters.putString("name", "Test Name");
parameters.putString("link", "http://www.mylink.com/");
parameters.putString("picture", imageUrl);
parameters.putString("display", "page");
String responsePost = facebook.request("me/feed", parameters, "POST");
String responsePagePost = facebook.request(FACEBOOK_PAGE_ID+"/feed", parameters, "POST");
Log.i(TAG, "responsePost = " + responsePost);
Log.i(TAG, "responsePagePost = " + responsePagePost);
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
What I am doing is I am posting a link to user's wall & also to my app's Facebook page.
This code works fine but problem is on Facebook page.The link which I post on Facebook page doesn't appear on page's wall but it appears on page's timeline in a box titled "Recent Posts by Others on MyApp Page".
I want the post should appear on wall rather than timeline.
What should I do I am not getting please help.
Facebook has changed all it's profiles to timelines now. Any facebook page will show posts by others as "Recent Posts by Others on MyApp Page". This behavior is controlled by facebook and not by your app. There is nothing you can do in this case.
Try this :
void postInfo()
{
try
{
Bundle parameters = new Bundle();
parameters.putString("message", messageFacebook);
parameters.putString("name", "Test Name");
parameters.putString("link", "http://www.mylink.com/");
parameters.putString("picture", imageUrl);
parameters.putString("display", "page");
facebook.dialog(this, "stream.publish", params,
new DialogListener() {
public void onFacebookError(FacebookError e) {
e.printStackTrace();
}
public void onError(DialogError e) {
// TODO Auto-generated method stub
e.printStackTrace();
}
public void onComplete(Bundle values) {
}
public void onCancel() {
}
});
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Authentication error when connect to Google Tasks API by HttpClient

I am trying to connect to the Google Tasks by using HttpClient but always get "Unauthorised HttpResponseException".
Here is my code inside the "run" method of the AccountManagerCallback
#Override
public void run(AccountManagerFuture<Bundle> result) {
// Get the result of the operation from the AccountManagerFuture.
Bundle bundle;
try {
bundle = result.getResult();
token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("#######", token); // token does have value
String url = "https://www.googleapis.com/tasks/v1/users/#me/lists?key=" + your_api_key;
HttpGet getRequest = new HttpGet(url);
getRequest.addHeader("client_id", your_client_id);
getRequest.addHeader("client_secret", your_client_secret);
getRequest.addHeader("Authorization", "OAuth " + token);
HttpClient httpclient = new DefaultHttpClient();
String responseBody = httpclient.execute(getRequest, new BasicResponseHandler()); // exception raised here
Log.i("###", responseBody); // cannot get the response here
} catch (OperationCanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am using Google APIs Level 10 emulator which does have access to the internet and I did add an google account into it.
Appreciate for any helps or suggestions to make my code work. Thanks.

post a photo to friend's wall on facebook with android

I am trying to post photo to my friend's wall.. I can do it as well but there is a problem. Anyone cant see it. I mean this photo shows only friend's wall.. not mine or not others friend.. I used some permissions but maybe I missed something..
private static final String[] PERMS = new String[] {"manage_pages", "read_insights", "user_checkins", "user_birthday","friends_birthday","email","user_photos","publish_checkins","publish_actions", "read_stream" ,"publish_stream","read_friendlists"};
and here is my codes..
public void postPhoto() {
String accessToken = mFacebook.getAccessToken();
params.putString("message", message);
params.putString("target_id", friendID);
params.putString(Facebook.TOKEN, accessToken);
response= mFacebook.request(friendID" + "/photos", params, "POST");
response = "{\"data\": [" + response + "] }";
String photoID = GetIDPhoto(response);
setTag(photoID, friendID);
}
private void setTag(String photoID,String friendID) {
String relativePath = photoID + "/tags/" + friendID;
Bundle params = new Bundle();
params.putString("x", "30");
params.putString("y", "30");
String response ="";
try {
response = mFacebook.request(relativePath, params, "POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), response, 3000).show();
}
Where is my mistake?
This seems to be your first problem, I haven't gone through the rest yet, I stopped at this:
response= mFacebook.request(friendID" + "/photos", params, "POST");
response = "{\"data\": [" + response + "] }";
Unless I'm mistaken, you seem to have some issues with your quotes.

facebook status update

I am having problem in updating status on facebook. I am using Facebook sdk, the problem is that my status is getting post but not showing text. Here is my code ---->
public class NetRockersUpdate extends AsyncTask{
#Override
protected String doInBackground(String... msg) {
// TODO Auto-generated method stub
nra = (NetRockersApp)getApplication();
String result = "Status Posted Successfully";
Bundle parameters = new Bundle();
parameters.putString("test", msg[0]);
parameters.putString("method", "stream.publish");
try {
nra.facebook.request(parameters);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
Check all the below post : I explained how to Post on User wall:
Post on User's Facebook Wall
Post to user Wall with Description,title,caption and link
Post on Wall..
you don't have a "message" key in your parameter.

Categories

Resources