tia
tia

Reputation: 1014

how to authenticate to REST web service from Android app?

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!

Upvotes: 2

Views: 4909

Answers (2)

Julian A.
Julian A.

Reputation: 11470

You may also want to check out the SimpleSyncAdapter sample project (included with Android SDK) to see how Android's account manager is used to handle authentication and credential storage. I believe this is the way your use-case is meant to be supported in Android.

Upvotes: 0

Chris Fei
Chris Fei

Reputation: 1317

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.

Upvotes: 1

Related Questions