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()");
}
});
}
Related
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
Please help me in getting the user details for the current user in the BoxApi v2 for android
The code which I am using the user details is as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BoxAndroidClient client = null;
if (Activity.RESULT_OK != AUTH_REQUEST) {
Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
} else {
BoxAndroidOAuthData oauth = data
.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
BoxAndroidOAuthData moath = data
.getParcelableExtra(OAuthActivity.USER_SERVICE);
client = new BoxAndroidClient(HelloWorldApplication.CLIENT_ID,
HelloWorldApplication.CLIENT_SECRET, null, null);
client.authenticate(oauth);
accestoken = oauth.getAccessToken().toString();
System.out.println("AUTHDATA" + oauth.getAccessToken().toString());// client.getUsersManager().getCurrentUser(requestObj).toString()
// + oauth.getAccessToken().toString());
BoxUser user = new BoxUser();
System.out.println("ID" + user.getId());
if (client == null) {
Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
} else {
((HelloWorldApplication) getApplication()).setClient(client);
Toast.makeText(this, "authenticated", Toast.LENGTH_LONG).show();
}
}
BoxDefaultRequestObject requestObject = null;
List<BoxUser> userList = null;
try {
userList = client.getUsersManager().getAllEnterpriseUser(
requestObject, null);
} catch (BoxRestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BoxServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthFatalFailureException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (BoxUser usr : userList) {
System.out.println("Addr: " + usr.getAddress());
System.out.println("Name : " + usr.getName());
System.out.println("Login : " + usr.getLogin());
}
}
I am getting the exception as networkonmainthreadexception...
Can anybody please help me?
Getting current user info in the raw api is done with a call to GET /users/me
Not sure what that looks like in the Java, but probably something more like :
user = client.getUsersManager().getCurrentUser
Make sure your access token is up to date. Also, looking at your code, why have you set
BoxDefaultRequestObject requestObject = null;
Instead do:
BoxDefaultRequestObject requestObject = new BoxDefaultRequestObject()
Otherwise the box library will get a null pointer exception. It gets the JSON parser from the request object.
Let me know if this works.
I'm facing with an intermittent application flow issue.
Here, I've got a Login screen where I'm able to login for the first time. But when I'm logging out and/or re-loging in, I'm unable to traverse further. As a work around, I need to uninstall my application and reinstall it and the flow is ok.
Can anyone please guide me on the possible issue.
LoginActivity.java
GetWebServiceManager passwordExpiryWSManager = new GetWebServiceManager();
//to check network connectivity (data connection/Wi-Fi)
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInformation = connectionManager.getActiveNetworkInfo();
if (networkInformation != null && networkInformation.isConnected() == true) {
try {
// passwordExpiryString = passwordExpiryWSManager.execute("http://172.25.164.143:8088/api/Login/GetExpiredPassword/?lContactKey=" + username.getText().toString()).get();
passwordExpiryString = passwordExpiryWSManager.execute(AppConstants.URL + "/Login/GetExpiredPassword/?lContactKey=" + username.getText().toString()).get();
jsonObjectPassExpiry = new JSONObject(passwordExpiryString);
jsonArrayPassExpiry = jsonObjectPassExpiry.getJSONArray("LstLoginDetail");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* Converting JSON string response from Webservice into Java Object
* */
if (jsonArrayPassExpiry != null) {
Gson gson = new Gson();
LoginTO[] loginExpTO = gson.fromJson(jsonArrayPassExpiry.toString(), LoginTO[].class);
loginListPassExpiry = Arrays.asList(loginExpTO);
intent = getIntent();
if (loginListPassExpiry.get(0).getlSuccess() == 1) {
intent.setClass(getApplicationContext(), PortfolioSummaryFragmentActivity.class);
// intent.putExtra("sToken", loginList.get(0).getsToken());
startActivity(intent);
} else {
intent.setClass(getApplicationContext(), PasswordExpiryActivity.class);
// intent.putExtra("sToken", loginList.get(0).getsToken());
startActivity(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Service is not responding. Please try again later!!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Network connection unavailable. Please check your data plan or Wi-Fi connection!!", Toast.LENGTH_SHORT).show();
}
Logout.java
public class LogoutActivity extends Activity {
private TextView successResponse;
private Intent intent;
private Button btnLLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logout_success);
successResponse = (TextView) findViewById(R.id.txtvwLSuccessMessage);
btnLLogin = (Button) findViewById(R.id.btnLLogin);
intent = getIntent();
successResponse.setText(intent.getStringExtra("successResponse"));
}
public void Login(View v) {
btnLLogin.setTextColor(getResources().getColor(R.color.black));
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
#Override
public void onBackPressed() {
finish();
}
}
please use intent = new Intent(LogoutActivity.this, LoginActivity.class); instead of intent = new Intent(this, LoginActivity.class);
beside this use
most important
Add android:launchMode="singleTask " to the activity element in your manifest for Activity LoginActivity
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?
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.