views:

136

answers:

1

I need to pull data from a REST web service in my android app. The web service requires authentication.
I need to first call a login method, which will return me an authToken and JSESSIONID as part of the response header. I need to pass these items back with every request. I'm currently using: org.apache.http.impl.client.DefaultHttpClient.DefaultHttpClient() to call the login method. I can see the authToken and JSESSIONID are being returned in the response header. Are there any existing tools or classes that might provide some sort of management functionality that I can easily integrate with my android app?

thanks!

A: 

You can store the values as key/value pairs using SharedPreferences and then create some kind of convenience method that appends those values to your web requests. Once you get these values from the response store them with,

SharedPreferences apiVals = getSharedPreferences("apiVals", MODE_PRIVATE);
apivals.edit()
       .putString("authToken", authToken)
       .putString("jSessionId", jSessionId)
       .commit();

and to retrieve them,

SharedPreferences apiVals = getSharedPreferences("apiVals", MODE_PRIVATE);
String authToken = apiVals.getString("authToken", null);

Check out http://developer.android.com/reference/android/content/SharedPreferences.html for more information.

cfei