开发一个网站的手机客户端改如何处理用户信息的获取呢。

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

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

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

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

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

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

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

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

    

  1. <tr><td align="right">验证码:</td><td><input type="text" id="code" name="code" size="8" class="input-text">
  2. <img id='code_img' onclick='"http://blog.51cto.com/viewpic.php?refimg=" + this.src="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='>
  3. </td></tr> 

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

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

 

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

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

 

  1. private Bitmap getCodeImage() throws ClientProtocolException, IOException 
  2.    { 
  3.     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(); 
  4.     HttpClient client=myclient.getclient(); 
  5.     HttpGet httprequest=new HttpGet(CodeImageUrl); 
  6.     HttpResponse httpResponse=client.execute(httprequest); 
  7.     if(httpResponse.getStatusLine().getStatusCode()==200
  8.     { 
  9.         byte[] data=EntityUtils.toByteArray(httpResponse.getEntity()); 
  10.         Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); 
  11.         Log.i("height", bitmap.getHeight()+""); 
  12.         return bitmap; 
  13.     } 
  14.     else 
  15.         return null
  16.      
  17.    } 

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

HttpClient client=myclient.getclient();

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

 

  1. public class myclient { 
  2.     private static HttpClient client = new DefaultHttpClient(); 
  3.     private myclient() { 
  4.     } 
  5.     public static HttpClient getclient() 
  6.     { 
  7.         return client; 
  8.     } 
  9.  

具体不做解释。

 

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

 

  1. private void login(String username,String password,String code) throws ClientProtocolException, IOException 
  2.    { 
  3.     String loginUrl="http://shizhikou.yingxun.org/index.php?m=member&c=index&a=login"
  4.     HttpPost httpPost=new HttpPost(loginUrl); 
  5.     List<NameValuePair> params=new ArrayList<NameValuePair>(); 
  6.     params.add(new BasicNameValuePair("username", username)); 
  7.     params.add(new BasicNameValuePair("password", password)); 
  8.     params.add(new BasicNameValuePair("code", code)); 
  9.     params.add(new BasicNameValuePair("dosubmit""")); 
  10.     httpPost.setEntity(new UrlEncodedFormEntity(params)); 
  11.     HttpResponse httpResponse=myclient.getclient().execute(httpPost); 
  12.     if(httpResponse.getStatusLine().getStatusCode()==200
  13.         { 
  14.     //Log.i("out",EntityUtils.toString(httpResponse.getEntity())+" "); 
  15.         //show.setText(EntityUtils.toString(httpResponse.getEntity())); 
  16.     Toast.makeText(SzkloginActivity.this"ok",Toast.LENGTH_SHORT).show(); 
  17.         } 
  18.     else 
  19.         Toast.makeText(SzkloginActivity.this"no111",Toast.LENGTH_SHORT).show(); 
  20.          
  21.    } 

详情内容见代码。

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

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

代码如下:

 

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

结果如下:

 

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

结果如图所示:顺利登陆到相关页面,然后用户名,密码也都展现出来