Android的:如何存储cookie? [英] Android: How to store cookies?

查看:154
本文介绍了Android的:如何存储cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,每个活动都充满了从一个XML文件,该文件是某个地方在网络上的数据。该网站提供了这些文件有一个登录机制,与饼干的作品。

In my Android app, each activity is filled with data from an xml file which is somewhere on the web. The website providing these files has a login mechanism, that works with cookies.

我知道如何使一个HTTP请求到登录页面,并收到一个cookie。我不知道的是,我怎么可以存放在两个其他活动重复使用它,当应用程序在下次启动。这个cookie的有效期为一年,所以我的应用程序的用户应了整整一年一次登录,然后永远不再。

I know how to make a HTTP Request to the login page and receive a cookie. What I don't know is, how I can store it to re-use it in both other activities AND when the app is started the next time. The cookie is valid for a year, so the user of my app should log in once and then never again for a whole year.

我该怎么办呢?我用Google搜索了很多,但无论是我用错了关键字或有在互联网上没有简单的解决方案。我希望有人在这里可以帮助我。

How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.

提前最好的问候和感谢,扬奥利弗

Best regards and thanks in advance, Jan Oliver

推荐答案

它看起来像Android正在使用默认的内存中执行,所以你需要创建自己的持久性cookie存储区。

It looks like Android is using the default in memory implementation so you will need to create your own persistent cookie store.

本Java教程概述了创建自己的持久性存储采取的默认实现的优势。 <一href="http://download.oracle.com/javase/tutorial/networking/cookies/custom.html">http://download.oracle.com/javase/tutorial/networking/cookies/custom.html

This Java Tutorial outlines creating your own persistent store taking advantage of the default implementation. http://download.oracle.com/javase/tutorial/networking/cookies/custom.html

样品有两个待办事项的存储(读/写)对于存储,我只想使用共享preferences只存储你所需要的会话cookie,而不是坚持任何人。

The sample has two todo's for storage (read/write) For storage I would just use SharedPreferences to store just the session cookie that you need and not persist any others.

该示例使用一个关闭钩子这是不是你想在Android的东西。代替运行()和钩我只想有一个新的公共方法,坚持(),节省了你想要什么,尽管这需要你坚持()商店的手。

The sample uses a shutdown hook which is not what you want in Android. In place of run() and the hook I would just have a new public method persist() that saves what you want, though that requires that you persist() the store by hand.

既然你只说事,你可以将它们保存在添加一个或两个cookie(...)

Given that you only have one or two cookies that matter you could save them in the add(...)

这篇关于Android的:如何存储cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆