I am using following source https://github.com/liveservices/LiveSDK-for-Android.
Do someone know how to get shared preferences after the LiveAuthClient process. I have to get
REFRESH_TOKEN_KEY and COOKIE_DELIMITER from the sharedprefences file and clear it after saving it in a database. The aim is to save this to values in a database to login with multiple skydrive accounts.
Any ideas would be helpful. Thank you.
If Someone wants to know use:
/** Name of the preference file */
public static final String FILE_NAME = "com.microsoft.live";
public static final String COOKIES_KEY = "cookies";
public static final String REFRESH_TOKEN_KEY = "refresh_token";
SharedPreferences preferences = getSharedPreferences(SDriveConstants.FILE_NAME, Context.MODE_PRIVATE);
String refresh_tkn_key = preferences.getString(SDriveConstants.REFRESH_TOKEN_KEY, "");
String cookies_key = preferences.getString(SDriveConstants.COOKIES_KEY, "");
Related
Having a little trouble figuring this one out. I have two values stored in my Config class as shared preferences, an email address and a URL. This will parse JSON to a ListView from my server. I'm trying to concatenate them together but it is not working.
Config.java
public class Config {
//URL to JSON API
public static final String GET_ORDERS_URL = "http://192.568.8.245/android/json3.php?user_email=";
//Storing current user
public static final String EMAIL_SHARED_PREF = "user_email";
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
// Concatenated URL to JSON API
private static final String baseurl = Config.GET_ORDERS_URL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Fetching email from shared preferences
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
String user_email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF,"Not Available");
//Concatenated JSON API URL
String url = baseurl + user_email;
//Initializing Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_back);
toolbar.setTitle(user_email);
}
I know user_email is being stored because I am calling it within MainActivity and setting the title of my toolbar with it. Replacing user_email with an actual email results in success, obviously I don't want to do that. Any idea what I need to do?
use android UrlEncoder class to make correct url
this is the best way. Using manual concatenate can result in malformed requests.
and also you can build URI like this
String uri = Uri.parse("http://...")
.buildUpon()
.appendQueryParameter("key", "val")
.build().toString();
Get the values and call concatStrings(str1,str2);
Use something like this
String concatStrings(String string1,String string2){
StringBuilder strBuilder=new StringBuilder();
strBuilder.append(string1);
strBuilder.append(string2);
return strBuilder.toString();
}
Note: the OP has applied this answer already to the code in it's question. It was meant for version 1 of the question.
The concatenation is working and results in the string:
"http://192.568.8.245/android/json3.php?user_email=user_email"
because you concatenate with the shared preferences key, not the value.
You want to concatenate with the value retrieved from shared preferences instead
String user_email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF);
String url = Config.GET_ORDERS_URL + user_email;
I am using Shared preferences for the first time and getting errors.
my code is like this :
public class MainActivity extends Activity {
static final String ONE = "";
static final String TWO = "";
private static SharedPreferences mSharedPreferences;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences.Editor edi = mSharedPreferences.edit();
edi.putString(ONE, "1");
edi.putString(TWO, "2");
edi.commit();
String one = mSharedPreferences.getString(ONE,"1");
String two = mSharedPreferences.getString(TWO,"2");
System.out.println("Your Numbers: "one+ " " + two);
}
}
Expected Output:
Your Numbers: 1 2
Console Output:
Your Numbers: 2 2
I can't figure out what i am doing wrong in it. Share your views.
You need to add some string to the names/keys. Currently both key names are blank and hence your code is overwriting the same preference value
.
Change the static strings as follows and it should work fine.
static final String ONE = "one";
static final String TWO = "two";
Also try using a helper class to make things simpler with shared preferences. Here is one that i wrote: Android-SharedPreferences-Helper
Because of this:
static final String ONE = "";
static final String TWO = "";
change it to:
static final String ONE = "One";
static final String TWO = "Two";
U need unique values for every preference.
In your case the ONE gets overridden by the TWO.
Extra info
If u look in the android docs here you will see that putString requires two parameters:
key : String: The name of the preference to modify.
value : String: The new value for the preference.
and if u than look at getString here you will notice that it also has two parameters, both the same as putString:
key : String: The name of the preference to retrieve. -- important
defValue : String: Value to return if this preference does not exist.
The name/key is the part that let the get part know from which preference it needs to get the value.
Hope this will make things a bit clearer for u!
Both the strings are empty
static final String ONE = "";
static final String TWO = "";
It should be like :
static final String ONE = "one";
static final String TWO = "two";
I would like to know how(if possible) to send my String from my Shared preferences, to an array of Strings I have so that my other class will be able to read it so the images can be displayed.
I know that getting "imgUrl" will give me the correct image URL corresponding to the image that is being viewed.
I'm really stuck on how I would be able to achieve this, any help would be great..
Class:
case R.id.FavouriteWallpaper:
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("imgUrl", mImageUrl);
editor.commit();
break;
}
return super.onOptionsItemSelected(item);
}
//Somehow get "imgUrl" from Sharedprefs to be displayed in this format:
public static final String[] ImageFavs = new String[] {
"www.URLTOBEDISPLAYEDHERE.jpg"
};
Have you tried this.?
String uri=prefs.getString("imgUrl", "default uri");
In case the user login to app and then switch to other app without logout, how we manage the remember of the credential so once the user back to the previous application he don't need to type credential details again (user/pass)- (Please provide answer with theory also)
Define some statics to store the preference file name and the keys you're going to use:
public static final String PREFS_NAME = "MyPrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
You'd then save the username and password as follows:
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, username)
.putString(PREF_PASSWORD, password)
.commit();
So you would retrieve them like this:
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
String username = pref.getString(PREF_USERNAME, null);
String password = pref.getString(PREF_PASSWORD, null);
if (username == null || password == null) {
//Prompt for username and password
}
Alternatively, if you don't want to name a preferences file you can just use the default:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
You can use applicationPreference for storing the username and password. Google for it, you can easily get the code for it. But for security reason, always try to save such information in encrypted form. And while retrieving info from applicationPrefernces, you have to decrypt it. here is link http://www.androidsnippets.com/encryptdecrypt-strings
Actually I want to know, how can I read and write URI using sharedPreferences. I mean to say that suppose I have an int value, e.g.:
int x = 10;
Then for write date using sharedPrferences I used following code
SharedPreferences preferencesWrite = getSharedPreferences("myPreferences", 0);
SharedPreferences.Editor editor = preferencesWrite.edit();
editor.putInt("value", x);
editor.commit();
And similarly for reading data code I used is
x = preferencesRead.getInt("value", 0);
Now if I declared Uri calenEvent;
Then how can I read and write it to the register? If anyone knows, please help me to solve this out.
Covert URI to string
String uriStr = uri.toString();
You can save this string in SharedPreference easily.
and later reconstruct URI from the string
URI uri = new URI(uriString);