I am using facebook.authorize(activity,new Dialog(...)) in
public class BirthdayActivity extends Activity {
...
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
It works perfectly here, but when I do the same in
public class StartBirthdayApplication extends Service {
...
public void onStart(Intent intent, int startId) {
facebook.authorize(this, new DialogListener() {
public void onComplete(Bundle values) {
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
...
}
I get the error
Key message expected byte[] but value was a java.lang.String.The default value <null>was returned.
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.String
What is going wrong?
just pass access token in request.
working correct.
Related
I want Image and Text share together in Facebook through android application..... i am getting text by this method.
public void postToWall() {
// post on user's wall.
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onComplete(Bundle values) {
}
#Override
public void onCancel() {
}
});
}
Now ant add image together with text but want without intent functionality
I am trying to integrate Facebook ** into my android app. I want to implement **like and share ** facility on **facebook. But i am getting error.
please help me to solve this.
Also I want to know the process of creating New App on Facebook.
I am using code from GitHub. My code is -
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("333778590046892");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
private void save(String token){
Toast.makeText(this, "token"+token, Toast.LENGTH_LONG).show();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
}
I am getting the following error while running the above code -
Dialog Errorcom.facebook.android.DialogError: The connection to the server was unsuccessful.
try this Single Sign On (SSO) Using Android Native Client For Facebook .
paste this
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
instead of this
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
I am using facebook connect in my android app. It is basically a form. There is a facebook login button on the page. I want the information on the form to be filled in when thee user logs in using Facebook login.
I am trying the following code:
public void loginFB() {
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
getlogininfo();
}
public void getlogininfo(){
mAsyncRunner.request("me", new RequestListener(){
#Override
public void onComplete(String response, Object state) {
try{
Log.d("Profile", response.toString());
JSONObject json = Util.parseJson(response);
final String fname1 = json.getString("first_name");
final String lname1 = json.getString("last_name");
final String email = json.getString("email");
MintPaymentsActivity.this.runOnUiThread(new Runnable(){
public void run() {
fname.setText(fname1);
lname.setText(lname1);
emailadd.setText(email);
}
});
}
catch(JSONException e){
Log.w("This", "eror");
}
}
#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
}
});
}
/*FB --- */
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
basically, when the user clicks the button, faceboook asks for login and then collects the JSON data to display the name and email of the logged in user.
It gives me the following error:
05-30 21:52:40.598: E/AndroidRuntime(665): FATAL EXCEPTION: Thread-13
05-30 21:52:40.598: E/AndroidRuntime(665): com.facebook.android.FacebookError: An active access token must be used to query information about the current user.
05-30 21:52:40.598: E/AndroidRuntime(665): at com.facebook.android.Util.parseJson(Util.java:279)
05-30 21:52:40.598: E/AndroidRuntime(665): at com.Myapp.Myapp$2.onComplete(Myapp.java:117)
05-30 21:52:40.598: E/AndroidRuntime(665): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)
When I put an extra button to fill in the details, so that the getlogininfo() is implemented on a button click (after logging in) it works fine. But i want it to be filled in as soon as the user clicks the facebook login button.
It should be:
public void loginFB() {
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
this.getlogininfo();
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
You want to execute getLogininfo only after the authentication has completed.
Also, I suggest that you handle the errors as well, at least log it so you'll know when developing.
I have 2 activities in my android application. On the first one, I ask the user to login with facebook. after the user logs in, i collect the user data such as email, name and call a new activity passing these parameters to it. below is my facebook authorize method:
public void loginFB(final View v)
{
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
this.getlogininfo(v);
}
private void getlogininfo(View v) {
// TODO Auto-generated method stub
logininfo(v);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
Below is my logininfo() method:
public void logininfo(final View v){
mAsyncRunner.request("me", new RequestListener(){
#Override
public void onComplete(String response, Object state) {
try{
Log.d("Profile", response.toString());
JSONObject json = Util.parseJson(response);
final String fname1 = json.getString("first_name");
final String lname1 = json.getString("last_name");
final String email = json.getString("email");
Intent fbLogged = new Intent();
Bundle passData = new Bundle();
passData.putString("fname", fname1);
passData.putString("lname", lname1);
passData.putString("email", email);
fbLogged.putExtras(passData);
fbLogged.setClass(v.getContext(), RequestFb.class);
startActivity(fbLogged);
}
catch(JSONException e){
Log.w("This", "eror");
}
}
#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
}
});
}
So, my new activity is starting on OnComplete() of getting the user data.
This works perfectly, but when the user clicks login, and logs in with facebook, the first activity page remains on the screen for a few seconds and then the next activity is called. there is a lag. How can I fix the lag? When the user clicks login and after the login is authorized, it should take the user to directly the second activity. Any suggestions?
Thanks!
It's simple, you are running the fb graph request in a new thread (using the AsyncRunner) but only when that request is completed you start the new activity and that's why you get that "lag".
You should run the graph request in the new activity instead of the first one, something like:
public void loginFB(final View v) {
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
Intent fbLogged = new Intent(v.getContext(), RequestFb.class);
startActivity(fbLogged);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
public class RequestFb extend Activity {
protected void onCreate(Bundle savedInstanceState) {
Facebook facebook = new Facebook("YOUR_APP_ID");
AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(facebook);
asyncRunner.request("me", new RequestListener(){
try {
final JSONObject json = Util.parseJson(response);
final String fname1 = json.getString("first_name");
final String lname1 = json.getString("last_name");
final String email = json.getString("email");
runOnUiThread(new Runnable() {
public void run() {
// use the data
}
});
}
catch(JSONException e) {
Log.w("This", "eror");
}
});
}
}
I'm using the tutorial code...
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class FacebookSSO extends Activity {
Facebook facebook = new Facebook("ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
I'm wondering... once the user has authenticated how would I store the access token? couldn't see any mention of it on the tutorial.
facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
String token=facebook.getAccessToken(); //get access token
save(token);
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
private void save(String token){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
i.e save it in sharedpreferences
FYI. facebook.authorize had deprecated since Facebook SDK 3
Try in this way
#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
}