//i got this error sometimes when using the feed dialog on facebook :com.facebook.android.DialogError: net::ERR_SPDY_PROTOCOL_ERROR
// sometime it works and sometimes it won't(loading appears the nothing happens) error is return on dialogerror function
here is the code :
Bundle parameters1 = new Bundle();
System.out.println("link>>>>"+srcLnk);
System.out.println("picture>>>>"+imagesrc);
System.out.println("name>>>>"+titlev);
System.out.println("description>>>>"+Html.fromHtml(url.toString()).toString());
parameters1.putString("link",srcLnk);
parameters1.putString("picture",imagesrc);
parameters1.putString("name",titlev);
parameters1.putString("caption","www.alsumaria.tv");
// parameters1.putString("description",url.toString().replaceAll("\\n", ""));
parameters1.putString("description",Html.fromHtml(url.toString()).toString());
facebook.dialog(activity,"feed",parameters1,
new DialogListener() {
#Override
public void onComplete(Bundle values) {
Toast.makeText(activity, "Posted successfully", Toast.LENGTH_SHORT).show();
}
#Override
public void onFacebookError(FacebookError error) {
error.printStackTrace();
}
#Override
public void onError(DialogError e) {
e.printStackTrace();
}
#Override
public void onCancel() {}
}
);
Related
I have to integrate Quickblox chat service in my app. I have done the integration part using its documentation but How so I set profile pics of user and get it to show in QBChatDialog. I have gone through this link but don`t think it will make my work. Please reply soon.
To upload or update avatar, please use the following snippet of code:
// just create any file
File avatar = ...;
// Upload new avatar to Content module
Boolean fileIsPublic = false;
QBContent.uploadFileTask(file1, fileIsPublic, null, new QBEntityCallback<QBFile>() {
#Override
public void onSuccess(QBFile qbFile, Bundle params) {
int uploadedFileID = qbFile.getId();
// Connect image to user
QBUser user = new QBUser();
user.setId(300);
user.setFileId(uploadedFileID);
QBUsers.updateUser(user, new QBEntityCallback<QBUser>(){
#Override
public void onSuccess(QBUser user, Bundle args) {
}
#Override
public void onError(QBResponseException errors) {
}
});
}
#Override
public void onError(QBResponseException errors) {
}
},new QBProgressCallback() {
#Override
public void onProgressUpdate(int progress) {
}
});
Now other users can see your avatar:
int userProfilePictureID = user.getFileId(); // user - an instance of QBUser class
QBContent.downloadFileById(userProfilePictureID, new QBEntityCallback<InputStream>(){
#Override
public void onSuccess(InputStream inputStream, Bundle params) {
}
#Override
public void onError(QBResponseException errors) {
}
}, new QBProgressCallback() {
#Override
public void onProgressUpdate(int progress) {
}
});
This is My LoginActivity.java
public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
#BindView(R.id.login_facebook)
Button loginFacebookBtn;
#BindView(R.id.privacy_policy)
TextView privacyPolicy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
Backendless.setUrl(AppConstant.SERVER_URL);
Backendless.initApp(this, AppConstant.APPLICATION_ID, AppConstant.API_KEY);
String userToken = UserTokenStorageFactory.instance().getStorage().get();
if (userToken != null && !userToken.equals("")) {
Backendless.UserService.isValidLogin(new DefaultCallback<Boolean>(this) {
#Override
public void handleResponse(Boolean isValidLogin) {
if (isValidLogin && Backendless.UserService.CurrentUser() == null) {
String currentUserId = Backendless.UserService.loggedInUser();
if (!currentUserId.equals("")) {
Backendless.UserService.findById(currentUserId, new DefaultCallback<BackendlessUser>(LoginActivity.this, "Logging in...") {
#Override
public void handleResponse(BackendlessUser currentUser) {
super.handleResponse(currentUser);
Backendless.UserService.setCurrentUser(currentUser);
ActivityHelper.start(LoginActivity.this, MainActivity.class);
finish();
}
});
}
}
super.handleResponse(isValidLogin);
}
});
}
loginWithFacebook();
privacyPolicy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, PrivacyPolicy.class));
}
});
registerDevice();
}
private void registerDevice() {
Backendless.Messaging.registerDevice(AppConstant.GCM_SENDER_ID, AppConstant.MESSAGING_CHANNEL, registerCallback());
}
private DefaultCallback<Void> registerCallback() {
return new DefaultCallback<Void>(this) {
#Override
public void handleResponse(Void response) {
super.handleResponse(response);
}
#Override
public void handleFault(BackendlessFault fault) {
super.handleFault(fault);
}
};
}
private void loginWithFacebook() {
loginFacebookBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onLoginWithFacebookButtonClicked();
}
});
}
private void onLoginWithFacebookButtonClicked() {
Map<String, String> facebookFieldsMapping = new HashMap<>();
facebookFieldsMapping.put("name", "name");
facebookFieldsMapping.put("email", "email");
List<String> facebookPermissions = new ArrayList<>();
facebookPermissions.add("email");
Backendless.UserService.loginWithFacebook(LoginActivity.this, null, facebookFieldsMapping, facebookPermissions, new SocialCallback<BackendlessUser>(LoginActivity.this) {
#Override
public void handleResponse(BackendlessUser response) {
ActivityHelper.start(LoginActivity.this, MainActivity.class);
finish();
}
#Override
public void handleFault(BackendlessFault fault) {
super.handleFault(fault);
Toasty.info(LoginActivity.this, "Try again, please ..", Toast.LENGTH_LONG).show();
}
}, true);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
#Override
public void onClick(View view) {
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}}
This is my facebook developers account settings
facebook screenshot 1
facebook screenshot 2
facebook screenshot 3
My Code is successfully running in android studio without any error.But when i run the application i am getting this error.
Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
error screenshot
I have created my LoginActivity.java bye taking this as a reference
https://backendless.com/documentation/users/android/index.html?users_facebook_login.htm
searched a lot in the web But Didn't get any relevant solution Can anybody help??
it looks like you are missing App Domain in your facebook settings (Settings > Basic > App Domains).
Try to add as your domain
api.backendless.com
If this is not working try to take a look at this https://backendless.com/documentation/manage/mgmt_social_settings.htm
i working facebook api in android. i successfully can sing in in facebook and try to check facebook user name
i wrote some code but i have
only the original thread that created a view hierarchy
exception
this is a my code:
public void LoginFacebook() {
mFacebook.authorize(this, mPermissions, new LoginDialogListener());
}
private final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionStore.save(mFacebook, getApplicationContext());
getProfileInformation();
}
public void onCancel() {
SessionEvents.onLoginError("Action Canceled");
}
#Override
public void onFacebookError(FacebookError error) {
SessionEvents.onLoginError(error.getMessage());
}
#Override
public void onError(DialogError error) {
SessionEvents.onLoginError(error.getMessage());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
#SuppressWarnings("deprecation")
#SuppressLint("SdCardPath")
public void getProfileInformation() {
mAsyncRunner.request("me", new BaseRequestListener() {
#SuppressLint("CommitPrefEdits")
#Override
public void onComplete(String response, Object state) {
Log.e("Profile", response);
String json = response;
try {
JSONObject profile = new JSONObject(json);
facebook_userid = profile.getString("id");
facebook_username = profile.getString("name");
facebook_username = facebook_username.replace("%20", " ");
fb_name.setText(facebook_username);
} 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) {
}
});
}
what am i doing wrong. i can sing in but i can't to show username in textview.
if anyone knows solution please help me
thanks
In your onComplete method you're trying to set the text of a UI element. Your onComplete method is getting called from a background(non main/UI thread). Any time you make a UI change, it must be on the UI thread. Try inserting a runOnUIThread call in your onComplete like this:
runOnUiThread(new Runnable() {
public void run() {
fb_name.setText(facebook_username);
}
});
Note: you will need to make variables declared outside the runnable final (ex: facebook_username)
Also note: most (if not all) web based calls will return on a background thread so you might need to use runOnUIThread frequently.
I followed this tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/ to connect Facebook to my Android application. Instead of having many buttons, I have a button that will be used for both login and logout.
1) The first time I run the class, I could login and then logout successfully. And when I click on the same button to login again, the login page would not appear, however the Toast text "LOGGING IN" appear, which is after loginToFacebook() function in my if-else. Hence, I assume, it should have run the facebook login page like the first time I run the class. But the login page does not appear.
What did I do wrong? And what should I do?
2) And how do I display the username in String fbLoggedIn after logged in instead of the text "CONNECTED!!" ?
public class FacebookActivity extends Activity{
private static String APP_ID = "";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Button backButton;
private String name = "CONNECTED!!";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_share);
facebook = new Facebook(APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);
RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
fbButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!facebook.isSessionValid())
{
System.out.println("Not Connected. Clicked and Login.");
loginToFacebook();
Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}
else
{
System.out.println("Connected. Logged Out.");
logoutFromFacebook();
Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
}
});
}
public void loginToFacebook() {
mPrefs = getPreferences(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", "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();
TextView fbUser = (TextView) findViewById(R.id.fbUser);
fbUser.setVisibility(View.VISIBLE);
String fbLoggedIn = name;
fbUser.setText(fbLoggedIn);
Toast.makeText(getApplicationContext(), "LOGGED IN AS " + name, Toast.LENGTH_LONG).show();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
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);
// 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) { }
});
}
//logout from Facebook
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
// User successfully Logged out
}
}
#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) { }
});
}
Just to be clearer, I set my if-else like this (extracted from the complete codes above).
RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
fbButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!facebook.isSessionValid())
{
System.out.println("Not Connected. Clicked and Login.");
loginToFacebook();
Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}
else
{
System.out.println("Connected. Logged Out.");
logoutFromFacebook();
Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
}
});
This is the behavior that Facebook wants you to take with their API.Your login method checks to see if you have already signed in once and that the facebook session is active. If the session is active then it sets the properties of the facebook object and connects it behind the sences so that the login page does not show again.
So you're not doing anything wrong, it's suppose to happen this way.
My code is as below:
public void logoutFromFacebook() {
mAsyncRunner.logout(this, new RequestListener() {
#Override
public void onComplete(String response, Object state) {
Log.d("Logout from Facebook", response);
if (Boolean.parseBoolean(response) == true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
// make Login button visible
btnFbLogin.setVisibility(View.VISIBLE);
// making all remaining buttons invisible
btnFbGetProfile.setVisibility(View.INVISIBLE);
btnPostToWall.setVisibility(View.INVISIBLE);
btnShowAccessTokens.setVisibility(View.INVISIBLE);
btnFbLogout.setVisibility(View.INVISIBLE);
}
});
}
}
#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) {
}
});
}
But it does not work and show's an error validating access Token. Can anyone give me a solution for this?
The reason why the error message says "invalid application id" is because you are either not providing an app id or you are providing an invalid app id.
You have to make sure you instantiate Facebook in the beginning of your code in the following manner
Facebook facebook = new Facebook(YOUR_APP_ID_HERE);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);