您的位置:首页 > 理论基础 > 计算机网络

android httpclient处理用户登录例子 广州JAVA培训

2012-06-01 15:59 549 查看
开发一个网站的手机客户端改如何处理用户信息的获取呢。

通过网上资料查看通过连接远程服务器数据库方式是不可取,第一个问题是安全问题。第二个问题是是否可以实现,众多网友测试,有的说不行,有的说可行。可谓众说纷纭。

为了保险起见,还是采用httpclient的方式来做比较好。

httpclient是相当于在移动客户端的一个浏览器。可以采用相关的方式实现post get等方法。下面看一下我做的小例子吧。

http://shizhikou.yingxun.org/index.php?m=member&c=index&a=login

这个地址为我测试用的登陆页面。

分析一下需要解决的问题。

首先网站登陆需要验证码。这个如何处理呢。

采用的解决办法就是获取验证码图片的地址。分析一下网页的代码。

<tr><td align="right">验证码:</td><td><input type="text" id="code" name="code" size="8" class="input-text">
<img id='code_img' onclick='"http://blog.51cto.com/viewpic.php?refimg=" + "http://blog.51cto.com/viewpic.php?refimg=" + this.src=\'#\'" //blog.51cto.com/viewpic.php?refimg=" + "http://blog.51cto.com/viewpic.php?refimg=" + this.src+"&"+Math.random()' src='http://shizhikou.yingxun.org/api.php?op=checkcode&code_len=4&font_size=14&width=84&height=24&font_color=&background='>
</td></tr>

从以上代码可以清晰的看到验证码图片的地址为:http://shizhikou.yingxun.org/api.php?op=checkcode&code_len=4&font_size=14&width=84&height=24&font_color=&background=

然后把地址输入到浏览器看一下 会发现 每次刷新浏览器验证码的图片都会变化。看一下安卓这部分的代码吧。





3个框 分别为用户名,密码,验证码。

private Bitmap getCodeImage() throws ClientProtocolException, IOException
{
String CodeImageUrl="http://shizhikou.yingxun.org/api.php?op=checkcode&code_len=4&font_size=14&width=84&height=24&font_color=&background="+Math.random();
HttpClient client=myclient.getclient();
HttpGet httprequest=new HttpGet(CodeImageUrl);
HttpResponse httpResponse=client.execute(httprequest);
if(httpResponse.getStatusLine().getStatusCode()==200)
{
byte[] data=EntityUtils.toByteArray(httpResponse.getEntity());
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
Log.i("height", bitmap.getHeight()+"");
return bitmap;
}
else
return null;

}

这是获取验证码的相关代码。内容也很简单。

HttpClient client=myclient.getclient();

本处采用了单例模式获取httpclient代如下。

public class myclient {
private static HttpClient client = new DefaultHttpClient();
private myclient() {
}
public static HttpClient getclient()
{
return client;
}

}

具体不做解释。

然后继续看一下如何做登陆的。

private void login(String username,String password,String code) throws ClientProtocolException, IOException
{
String loginUrl="http://shizhikou.yingxun.org/index.php?m=member&c=index&a=login";
HttpPost httpPost=new HttpPost(loginUrl);
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("code", code));
params.add(new BasicNameValuePair("dosubmit", ""));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse=myclient.getclient().execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode()==200)
{
//Log.i("out",EntityUtils.toString(httpResponse.getEntity())+" ");
//show.setText(EntityUtils.toString(httpResponse.getEntity()));
Toast.makeText(SzkloginActivity.this, "ok",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(SzkloginActivity.this, "no111",Toast.LENGTH_SHORT).show();

}

详情内容见代码。

通过登陆之后httpclient如通浏览器一样获取到了cookie值。保存在对象内。

通过测试访问以下用户中心看是否能够获取用户中心的页面

代码如下:

String CodeImageUrl="http://shizhikou.yingxun.org/index.php?m=member&c=index";
HttpClient client=myclient.getclient();
HttpGet httprequest=new HttpGet(CodeImageUrl);
HttpResponse httpResponse=client.execute(httprequest);
show.setText(EntityUtils.toString(httpResponse.getEntity()));

结果如下:





结果如图所示:顺利登陆到相关页面,然后用户名,密码也都展现出来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: