-2

I have 2 activities (login and webcall). Login stores user details (using Shared Preferences). I've generated all the classes from the webservice wsdl. In my webcall activity, I call a WebServiceSoapCall class i.e. WebServiceSoapCall webcall = new WebServiceSoapCall; this class passes parameters to another class like this: HttpTransportBasicAuth(url,password,username); Since these are regular classes and not activities (HttpTransportBasicAuth and WebServiceSoapCall ), I am not able to retrieve these parameters from my Shared preference within them:
SharedPreference mypref = getsharedpreference(SP_NAME,Contect.MODE_PRIVATE); getsharedpreference() doesn't work in classes only in activities. Right now I'm explicitly passing these parameters: HttpTransportBasicAuth("myurl","myusername","mypassword"); I want to avoid using this hardcoding for obvious reasons.

Tony Ray
  • 1
  • 1
  • 3
    Youll have to try and clarify that or just add the java classes because as for now you question is not clear. – crazyPixel Mar 28 '16 at 19:08
  • See this, from the ... Elena http://stackoverflow.com/a/23024962/5885018 – statosdotcom Mar 28 '16 at 19:10
  • I have tried using shared preferences just like in all the examples on this site and the tutorails online.. It works fine when you use them inside activities but not when you are trying to retrieve the information from a java class – Tony Ray Mar 28 '16 at 20:18
  • 1
    You have to pass a `Context` object into the Java class in order to get the SharedPrefereneces – OneCricketeer Mar 28 '16 at 20:20
  • hi Elena, when you use the getSharedPreference() from within a class, it doesn't work because it is not an activity..hence my problem :( – Tony Ray Mar 28 '16 at 20:22
  • Elena is the person who posted the answer in the other post. She isn't commenting here, so you can't say hi – OneCricketeer Mar 29 '16 at 13:02
  • thanks cricket_007 i managed to pass a context in the class and everything worked fine. I also had to make another class that stores and retrieves data to the sharedpreference. thanks all. – Tony Ray Mar 29 '16 at 13:04

1 Answers1

0

You can pass an extra parameter, a Context, and call context.getSharedPreferences()

Matias Elorriaga
  • 7,467
  • 3
  • 34
  • 53