Issue with Android IntentService - android

I am running a simple fql query inside the onHandleIntent() method of Intentservice . Dont know what the problem is , the query part is not getting executed only . here is my onHandleIntent() method
protected void onHandleIntent(Intent intent)
{
System.out.println("inside service method now");
String fqlQuery = "SELECT uid, username, online_presence, status FROM user WHERE uid = 100001024732884";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
System.out.println("inside the service now 4444");
Log.i(TAG, "Result: " + response.toString());
}
});
Request.executeBatchAsync(request);
}
Only first statement gets printed.
When i put same code in my MainActivity it works fine but inside IntentService it stops working . I am starting the service on a buttonclick.

Your problem is that IntentService will be finished and destroyed before your callback is called since it is running asynchronously. Instead of using a callback to get the Response, try using this instead.
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET);
Response response = request.executeBatchAndWait();

Related

How can I upload photo to facebook album from android app using following graph API?

I am new to android development. I am trying to post photo from an android app to user's facebook album. I added following code after I logged in the user with permissions "user_friends"
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
// This is my code for user login at app start
post = (Button)view.findViewById(R.id.button1);
mLoginManager = LoginManager.getInstance();
mLoginManager.registerCallback(mCallbackManager, mCallback);
mLoginManager.logInWithReadPermissions(this, Arrays.asList("user_friends")); //user_friends // publish_actions
Please guide me
I did a mistake .. Firstly, my question was incomplete .. I have found the issue .. Following seems to be working.
MainFragment.mLoginManager.logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
Bundle params = new Bundle();
params.putString("url", "http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg");
/* make the API call /
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/ handle the result */
Log.d("MainFragment:", "onCompleted mAccessToken = " + MainFragment.mAccessToken.getCurrentAccessToken());
}
}
).executeAsync();

access facebook page information

I want to get facebook page about, description, location, basic info etc. I have got the access token for page. I am getting likes of page like this:
public void getLikes (String id,final TextView likes,final int position)
{
Session session = Session.getActiveSession();
Bundle params = new Bundle();
params.putString("id", id);
params.putString("fields", "likes");
Request request = new Request(session, "search", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
try {
JSONObject res = response.getGraphObject().getInnerJSONObject().getJSONArray("data").getJSONObject(0);
if (res.length()>1){
likes.setText(String.valueOf(res.getInt("likes")));
Preferences.data.getData().get(position).setLikes(String.valueOf(res.getInt("likes")));
}
else {
Preferences.data.getData().get(position).setLikes("No Likes");
likes.setText("0");
}
} catch (Exception e) {
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
If I add more parameters in bundle, I get the value of last parameter added, what should I do. Facebook's documentation about graph api is not much clear.
This isn't an issue specific to the API, you're just only using one field of the response (.getJSONObject(0))
if you request multiple fields you need to look at each of them, currently you're only fetching the first

Android: Sharing images via Facebook API

I am trying to share an image via the android facebook API to facebook.
Sharing text and links works fine, uploading an image, too.
I found out, that I only can post images, if they are online or in the photoalbum of the user.
Bundle bundle = new Bundle();
bundle.putParcelable("picture", b);
Request request = new Request(session, "me/photos", bundle,
HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
String imageid = (String) response.getGraphObject()
.getProperty("id");
share(session, text, imageid);
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
imageid is a number like "123456789908765345678"
After the execution, "share" is called:
Bundle bundle = new Bundle();
bundle.putString("caption", "some text");
bundle.putString("description", "Imageid:"+imageid);
bundle.putString("link",
"https://link.de");
bundle.putString("name", "some text");
bundle.putString("place", imageid); //It doesn't work for me
new WebDialog.FeedDialogBuilder(this, session, bundle).build().show();
I don't know what I am doing wrong. I simply want so share this image.
Help please :)
You have to make another request with the id of the resource in order to get the url of the image you are trying to reference in the share dialog.
So you should do something like this:
Callback callback = new Callback() {
#Override
public void onCompleted(Response response) {
if (response.getGraphObject() != null) {
String imageUrl = (String) response.getGraphObject().getProperty("picture");
// call to your sharing method
share(session, text, imageUrl);
}
}
};
Request request = new Request(Session.getActiveSession(), imageid, null, HttpMethod.GET, callback);
request.executeAsync();
You can upload the photo to facebook album using
com.facebook.Request fbRequest = com.facebook.Request.newUploadPhotoRequest(session, image, callback);
fbRequest.executeAsync();

Android Facebook SDK - Get user events

i've being able to login and get the user infos from the api with this code:
Facebook facebook = new Facebook("XX");
AccessToken token = AccessToken.createFromExistingAccessToken("XX", new Date(facebook.getAccessExpires()),
new Date(facebook.getLastAccessUpdate()),
AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, Arrays.asList(permissions));
Session.openActiveSessionWithAccessToken(getActivity(), token, callback);
session = Session.getActiveSession();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
Object proper = user.getFirstName();
System.out.println(proper);
}
});
I'm using this fixed tokens cause its always come from the same user as some sort of a host but i need to get the user events and havent seen any documentation that shows how to do it, does anyone have any idea of how to get the user events that he created?
thank you!
I've managed to achieve what i want using a FQL Query with this code:
String fqlQuery = "SELECT eid, name, pic, creator, start_time FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid='XX' and rsvp_status='attending')";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Request request = new Request(session, "/fql", params, HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
System.out.println("Result: " + response.toString());
}
});
Request.executeBatchAsync(request);

How to get json response from facebook fql query in android

I am using an fql query to get the json response.I am new to this fql execution process.can I know in detail how to execute this query.I have seen some Listener class is using while getting the response,
SELECT likes.user_likes FROM stream WHERE post_id ='XXXXXXXXXXXXX_XXXXXXXXX'
Do I need to have anything to execute this and get the response?
Thanks in advance
private void doFQLQuery()
{
//get the active facebook session
Session session = Session.getActiveSession();
//create the fql query
String fqlQuery = "SELECT likes.user_likes FROM stream WHERE post_id ='XXXXXXXXXXXXX_XXXXXXXXX'";
//create a bundle for the parameters(your query)
Bundle params = new Bundle();
params.putString("q", fqlQuery);
//setup the request
Request request = new Request(session, "/fql", params, HttpMethod.GET, new Request.Callback()
{
public void onCompleted(Response response)
{
//when the request is completed output the results to the log
Log.i(TAG, "Result: " + response.toString());
}
});
//execute the request
Request.executeBatchAsync(request);
}
You can use this code to execute an fql query and get the response. Whatever processing you want to to do on the results should be done in the onCompleted method.

Categories

Resources