I want to post image along with text caption via facebook SDK.
I am referring to the solution posted here.
Android post picture to Facebook wall
However, I am getting the following error.
FATAL EXCEPTION: main
java.lang.NullPointerException
at java.net.URLDecoder.decode(URLDecoder.java:104)
at java.net.URLDecoder.decode(URLDecoder.java:48)
at com.facebook.android.Util.openUrl(Util.java:173)
at com.facebook.android.Facebook.request(Facebook.java:751)
at com.xxx.yyy.gui.share.ShareDialogFactory.shareThroughFacebook(ShareDialogFactory.java:170)
I had verified my code against the previous posted solution.
private static void shareThroughFacebook(final Context context, final File file, final int heartbeatForShare) {
final Facebook facebook = new Facebook("285243934881501");
// Login.
facebook.authorize((Activity)context, new String[] { "publish_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
android.util.Log.i("CHEOK", "onComplete");
}
#Override
public void onFacebookError(FacebookError error) {
android.util.Log.i("CHEOK", "onFacebookError");
}
#Override
public void onError(DialogError e) {
android.util.Log.i("CHEOK", "onError");
}
#Override
public void onCancel() {
android.util.Log.i("CHEOK", "onCancel");
}
});
android.util.Log.i("CHEOK", "STEP 1");
final byte[] data = JPEG2ByteArray(file);
final Bundle bundle = new Bundle();
bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
android.util.Log.i("CHEOK", "data length is " + data.length);
bundle.putByteArray("picture", data);
bundle.putString("message", "this-is-message");
bundle.putString("caption", "this-is-caption");
// I can reach till here!
android.util.Log.i("CHEOK", "STEP 2");
try {
// NPE happens right here!!!
facebook.request("me/photos", bundle, "POST");
} catch (FileNotFoundException e1) {
android.util.Log.i("CHEOK", "FileNotFoundException");
e1.printStackTrace();
} catch (MalformedURLException e1) {
android.util.Log.i("CHEOK", "MalformedURLException");
e1.printStackTrace();
} catch (IOException e1) {
android.util.Log.i("CHEOK", "IOException");
e1.printStackTrace();
}
android.util.Log.i("CHEOK", "STEP 3");
}
I can confirm my byte array is non-zero length. I do not receive any callback during authorize, not sure whether this is correct behaviour. Note, the code shareThroughFacebook is invoked from a Dialog.
Is there any other steps I had missed out?
Related
I am trying to send invitation from my app to facebook friends. everything is working fine, facebook invitation dialog is being shown, my frinds list is there, I am also receiving status in onComplete but on the other side (to whom I send invitation) nothing happens. not notification is being sent to my frinds. please let me know where I am wrong. below is my code.
private void inviteFriends() {
try {
Bundle params = new Bundle();
params.putString("title", "invite friends");
params.putString("message", "come join us!");
mFacebook.dialog(this, "apprequests", params, new DialogListener() {
#Override
public void onComplete(Bundle values) {
try {
// JSONObject eventResponse = new JSONObject(response);
// event_id = event.getString("id");
/*
* Log.i(TAG, "Event Response => "+eventResponse);
* Log.w("myapp", friends);
*/
System.out.println("fb invite response "
+ values.toString());
Toast.makeText(getApplicationContext(), "Request sent",
Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(),
// "New Event Created!!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
}
}
#Override
public void onFacebookError(FacebookError error) {
}
#Override
public void onCancel() {
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
}
}
now problem is that request is sent but notification comes in facebook app not in facebook browser?
You should use the Message Dialog to invite friends in an Android App, the App Requests are only for Canvas Games: https://developers.facebook.com/docs/apps/faq#invite_to_app
(See "If your app does not have a presence on Facebook Canvas")
More information: https://developers.facebook.com/docs/android/share#message-dialog
Hi i am using facebook to login to my app. I am unable to get user
birthday and user location from facebook. Below is the method used to get the details from user. I am using activity class for login.
private void loginFacebook() {
// TODO Auto-generated method stub
if (!facebook.isSessionValid()) {
final Session.OpenRequest request = new
Session.OpenRequest(LoginActivity.this);
facebook.authorize(this, new String[] {
"email","user_birthday","user_about_me","user_location"
}, new LoginDialogListener());
} else {
getProfileInformation();
}
}
public void getProfileInformation() {
// TODO Auto-generated method stub
try {
profile = Util.parseJson(facebook.request("me"));
mUserId = profile.getString("id");
Gloabalvariables.setUserID(mUserId);
mUserToken = facebook.getAccessToken();
mUserName = profile.getString("name");
mUserEmail = profile.getString("email");
JSONObject c=new JSONObject(profile.getString("location"));
mUserhomeTown=c.getString("name");
new insertintoServer().execute();
//getOnlineFreindList();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Name: " + mUserName "\nEmail+"+ mUserEmail, Toast.LENGTH_LONG).show();
}
});
} catch (FacebookError e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Here the problem is i can able to get user_birthday and user_location from the user only for the account which i created app id. For other account i am unable to get those details. i am struggle for two days. But i dint find what i did wrong in the code.
Go to Facebook Developer Console set permissions:
Facebook Graph API Explorer
There:
Application
Get Access Token: Here select your fieds which you want.
Search For Field
Submit
Save Session
I am trying to pass Facebook data from one activity to another.
The logs in activity 1 show that all the data was received (name, age etc.).
However, when I try to work with this data in activity 2, nothing happens. None of the logs at the bottom show up. Any idea what I'm doing wrong?
protected void handleFacebookData()
{
Intent intent = getIntent();
name = (String) intent.getSerializableExtra(Const.NAME);
Log.d(Profile.class.getSimpleName(), "Got FB name");
age = (String) intent.getSerializableExtra(Const.AGE);
Log.d(Profile.class.getSimpleName(), "Got FB age");
imgUrl = (String) intent.getSerializableExtra(Const.PIC);
x = BitmapFactory.decodeFile(imgUrl);
Log.d(Profile.class.getSimpleName(), "Got FB picture");
facebookData = true;
}
final Intent intent = new Intent(
MainActivity.this,
Profile.class);
intent.putExtra(Const.NAME,
getFacebookAccountName(userString));
intent.putExtra(Const.AGE,
getFacebookAge(userString));
intent.putExtra(Const.PIC,
getFacebookImage(userString));
startActivityForResult(intent,
REQUEST_FOR_RESULT_CREATE);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onError(DialogError de) {
Log.v(TAG, "********************** fb: onError()");
}
#Override
public void onFacebookError(FacebookError error) {
Log.v(TAG,
"********************** fb: onFacebookError()");
}
});
}
I have been trying to make wall post from multiple pages but when i try to make post from other pages except original facebook page, it is giving me an exception of java.lang.NullPointerException
The method i am calling and created in a Facebook page.
public void setConnection() {
mContext = this;
mFacebook = new Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
}
public boolean isSession() {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String access_token = sharedPrefs.getString("access_token", "x");
Long expires = sharedPrefs.getLong("access_expires", -1);
Log.d(TAG, access_token);
if (access_token != null && expires != -1) {
mFacebook.setAccessToken(access_token);
mFacebook.setAccessExpires(expires);
}
return mFacebook.isSessionValid();
}
public void getID()
{
Bundle bundle = new Bundle();
bundle.putString("fields", "birthday");
try {
mFacebook.request("me/friends", bundle);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error in FaceBook Friends List","Exception = "+e.getMessage());
e.printStackTrace();
}
if (isSession()) {
Log.d(TAG, "sessionValid");
try {
mFacebook.request("me/friends", bundle);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Error in FaceBook Friends List","Exception = "+e.getMessage());
e.printStackTrace();
}
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMISSIONS, new LoginDialogListener());
}
}
I am able to get the wall post dialog box but its showing "An error occurred. Please try again later".
Please help me out.
Thanks in advance.
I think the best way for you to achieve this would be to make an abstract class which extends the super class Activity, use this to hold all your facebook functions.
Then when you want to have an activity which has the facebook functionality then you would just extend this abstract class. If your not sure what I mean then take a look at the tutorial here, it goes through how to setup a basic Facebook Activity class. Hope this helps.
I have a problem. I want to use the facebook api and make a post to my wall without calling a dialog. Basically I have an app and I want people to be able to share the app, so I want to have a specific message to post. I keep getting a response of "Method not implemented". Heres the code for the post.
//I tried this also ->>String path = "http://graph.facebook.com/me/feed";
String path = "https://api.facebook.com/method/stream.publish";
Bundle b = new Bundle();
//And i tried this -> b.putString("access_token",facebook.getAccessToken());
b.putString("message", "this is just a test...");
try {
String ret = facebook.request(path, b);
Toast.makeText(fmasterActivity.this, ret, Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am assuming that you are doing that bit of code after the user successfully authenticates?
This bit of code worked for me:
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner;
private void onFacebookShare() {
mFacebook = new Facebook();
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionEvents.addAuthListener(new SampleAuthListener());
SessionEvents.addLogoutListener(new SampleLogoutListener());
}
private void postToFBWall() {
if(mFacebook.isSessionValid()){
shareVideoOnFB();
} else {
showDialog(DIALOG_FBOOK_LOGIN);
}
}
public void shareVideoOnFB(){
Bundle params = new Bundle();
params.putString("message", "This string will appear as the status message");
params.putString("link", "This is the URL to go to");
params.putString("name", "This will appear beside the picture");
params.putString("caption", "This will appear under the title");
params.putString("description", "This will appear under the caption");
params.putString("picture", "This is the image to appear in the post");
mAsyncRunner.request("me/feed", params, "POST", new RequestListener() {
public void onMalformedURLException(MalformedURLException e) {}
public void onIOException(IOException e) {}
public void onFileNotFoundException(FileNotFoundException e) {}
public void onFacebookError(FacebookError e) {}
public void onComplete(String response) {
logoutFacebook();
}
});
Toast.makeText(ShareActivity.this, "Posting to your Wall...", Toast.LENGTH_SHORT).show();
}
You can call onFacebookShare() in your activity's onCreate(), and then when the user presses whatever to indicate that s/he wants to share on Facebook, call postToFBWall(). Of course you have to add in handling to show the login dialog.