Android登录Web以及登录保持和cookie的使用方法

登录成功后可以使用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;
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值