Android登录Web以及登录保持和cookie的使用方法
来源:互联网 发布:centos中文 编辑:程序博客网 时间:2024/04/30 12:26
登录成功后可以使用SharedPreferences或者SQLite来保存用户信息,我用的是SQLite,登录的时候保存用户名和密码到SQLite中:
public String todooConnectLogin(Context context, final String m,final Map<String, String> params, final HttpMethod method) {String json = null;BufferedReader reader = null;try {DefaultHttpClient client = new DefaultHttpClient();HttpUriRequest request = getRequest(m, params, method);HttpResponse response = client.execute(request);CookieStore cookieStore = client.getCookieStore();List<Cookie> cookies = cookieStore.getCookies();TDCookieDao cookieDao = new TDCookieDao(context);for (Cookie cookie : cookies) {CookieObj cookieObj = new CookieObj();cookieObj.setName(cookie.getName());cookieObj.setValue(cookie.getValue());cookieDao.addCookie(cookieObj);}// if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)// {reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));// }StringBuilder sb = new StringBuilder();for (String s = reader.readLine(); s != null; s = reader.readLine()) {sb.append(s);}json = sb.toString();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println("-----login--json--->" + json);return json;}
打开其它页面时,需要向服务器发送带有登录成功的DefaultHttpClient,代码如下
public String todooConnect(Context context, final String m,final Map<String, String> params, final HttpMethod method) {String json = null;BufferedReader reader = null;try {DefaultHttpClient client = new DefaultHttpClient();TDCookieDao cookieDao = new TDCookieDao(context);List<CookieObj> cookieObjs = cookieDao.getAllCookies(CookieObj.FIELD_DOER_UID, false);BasicCookieStore newStore = new BasicCookieStore();for (CookieObj cookieObj : cookieObjs) {BasicClientCookie c1 = new BasicClientCookie(cookieObj.getName(), cookieObj.getValue());c1.setDomain("www.todoo.im");c1.setPath("/");newStore.addCookie(c1);}client.setCookieStore(newStore);HttpUriRequest request = getRequest(m, params, method);HttpResponse response = client.execute(request);// if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)// {reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));// }StringBuilder sb = new StringBuilder();for (String s = reader.readLine(); s != null; s = reader.readLine()) {sb.append(s);}json = sb.toString();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println("-----other--json--->" + json);return json;}
- Android登录Web以及登录保持和cookie的使用方法
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持、cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- android登录Web以及登录保持,cookie管理相关
- [Android实例] android登录Web以及登录保持,cookie管理相关
- HttpClient---cookie(保持登录)
- 简单安全的用cookie保持登录状态
- 基于Token的WEB后台登录认证机制(并讲解其他认证机制以及cookie和session机制)
- Android Cookie共享到WebView避免再次登录(保持登录状态)
- Android Cookie共享到WebView避免再次登录(保持登录状态)
- Android Cookie共享到WebView避免再次登录(保持登录状态)
- 库和框架区别
- {windows修改}之个人重装系统必做的一件事
- C++图像处理 -- 图像黑白调整应用
- linux主机安装pdo的教程
- SPRING.NET 1.3.2 学习16--使用depends-on
- Android登录Web以及登录保持和cookie的使用方法
- 关于erlang中makefile的编写
- Linux Sed & Awk
- findViewById 返回值为空
- 黑马程序员-包 jar包 javadoc
- Backbone.js
- iw的安装
- myelcipse中自动生成 配置文件
- linux apache PHP 以编译方式安装,编译参数详解析