开发者

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 serv开发者_开发百科ice 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜