Android系统中提供了SQLite数据库,用于本地的数据存储,App链接到网络就要用到专门的服务应用。目前已经存在了服务应用,想要开发一个Android移动应用用来享用已有的Web服务应用,这类似于传统的Client -Service。无论是B/S模式还是C/S模式,开发应用和业务处理,服务提供,数据存储等都不可缺少。Android很好的解决了这一问题,在现有的Web服务基础上,搭建客户端应用程序,共享已有的服务。

Apache开源项目中将Http协议访问做了一个二次封装,使得客户端应用程序访问Web服务器能够像浏览器访问一样方便(Apache-httpClient),正好Android SDK中提供了这个开源组件,为开发客户端应用程序访问服务器提供支持。

关于Android客户端访问Web服务器与传统的Web应用的架构如下图:

捣鼓了Android APP 访问Web服务器之后,最大的感受是C/S模式和B/S模式的概念开始模糊了,对访问模式的考虑在技术方面将淡化,而更多是用户的计算机处理能力,并发访问量,通信实时性,可靠性,数据传输量,安全性这些方面衡量。

想到关于B/S模式和C/S模式的纠结权衡在这个体验过后,应该不会再有太多技术可行性上的纠结,而更多的精力投入到对程序的运行环境,功能,用户体验等方面思考和设计。

关于享用已有的Web服务,开发Android客户端应用程序的大致流程总结如下:

1.对传统Web应用的MCV框架中的Servlet控制做相应的扩展,在不影响已有的系统的前提下,对客户端(浏览器,Android应用)请求进行判断,获取不同类型的请求响应信息。

例如下面代码:

        
  1. packageorg.estao.servelet;
  2. importjava.io.IOException;
  3. importjava.io.PrintWriter;
  4. importjavax.servlet.ServletException;
  5. importjavax.servlet.http.HttpServlet;
  6. importjavax.servlet.http.HttpServletRequest;
  7. importjavax.servlet.http.HttpServletResponse;
  8. importorg.estao.business.ActionBusiness;
  9. importorg.estao.business.ActionManager;
  10. importorg.json.JSONException;
  11. importorg.json.JSONObject;
  12. publicclassSettingServletextendsHttpServlet{
  13. /**
  14. *
  15. */
  16. privatestaticfinallongserialVersionUID=-4384397961898281821L;
  17. privateActionBusinessactionBusiness;
  18. publicvoiddestroy(){
  19. super.destroy();
  20. }
  21. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  22. throwsServletException,IOException{
  23. response.setContentType("text/html");
  24. response.setCharacterEncoding("UTF-8");
  25. doPost(request,response);
  26. }
  27. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
  28. throwsServletException,IOException{
  29. response.setContentType("text/html");
  30. response.setCharacterEncoding("UTF-8");
  31. PrintWriterout=response.getWriter();
  32. JSONObjectjsonObject=newJSONObject();
  33. booleanresult=actionBusiness.validSetting(jsonObject);
  34. try{
  35. jsonObject.put("Result",result);
  36. }catch(JSONExceptione){
  37. e.printStackTrace();
  38. }
  39. out.println(jsonObject.toString());
  40. out.flush();
  41. out.close();
  42. }
  43. publicvoidinit()throwsServletException{
  44. actionBusiness=ActionManager.getAppBusiness().getActionBusiness();
  45. }
  46. }

上面代码是获得JSON格式对象,作为响应信息。

2.在Android应用中以Http协议的方式访问服务器,使用Apache-httpclient开发包,或者进行适用于应用的再次封装。

例如下面代码:

        
  1. packageorg.estao.util;
  2. importjava.io.IOException;
  3. importjava.util.ArrayList;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importorg.apache.http.HttpResponse;
  7. importorg.apache.http.NameValuePair;
  8. importorg.apache.http.client.ClientProtocolException;
  9. importorg.apache.http.client.HttpClient;
  10. importorg.apache.http.client.entity.UrlEncodedFormEntity;
  11. importorg.apache.http.client.methods.HttpGet;
  12. importorg.apache.http.client.methods.HttpPost;
  13. importorg.apache.http.impl.client.DefaultHttpClient;
  14. importorg.apache.http.message.BasicNameValuePair;
  15. importorg.apache.http.util.EntityUtils;
  16. /**
  17. *
  18. *@authorAjax
  19. *
  20. *@messageJustForJSONObjectTransport
  21. *
  22. */
  23. publicclassHttpUtil{
  24. //创建HttpClient对象
  25. publicstaticfinalHttpClienthttpClient=newDefaultHttpClient();
  26. //访问Web服务器基础路径
  27. publicstaticfinalStringBASE_URL="http://10.43.10.108:8080/estao/";
  28. /**
  29. *GET方式无参数请求
  30. *
  31. *@param发送url请求
  32. *@return服务器相应的字符串
  33. *@throwsIOException
  34. */
  35. publicstaticStringgetRequest(Stringurl){
  36. HttpGetget=newHttpGet(url);
  37. HttpResponsehttpResponse=null;
  38. Stringresult=null;
  39. try{
  40. //发送GET请求
  41. httpResponse=httpClient.execute(get);
  42. //服务器端返回相应
  43. if(httpResponse.getStatusLine().getStatusCode()==200){
  44. //获取服务器相应的字符串
  45. result=EntityUtils.toString(httpResponse.getEntity());
  46. }
  47. }catch(ClientProtocolExceptione){
  48. e.printStackTrace();
  49. }catch(IOExceptione){
  50. e.printStackTrace();
  51. }
  52. returnresult;
  53. }
  54. /**
  55. *POST方式带参数请求
  56. *
  57. *@param发送url请求
  58. *@paramrawParams
  59. *@return服务器相应的字符串
  60. */
  61. publicstaticStringpostRequest(Stringurl,Map<String,String>rawParams){
  62. HttpPostpost=newHttpPost(url);
  63. HttpResponsehttpResponse=null;
  64. Stringresult=null;
  65. List<NameValuePair>params=newArrayList<NameValuePair>();
  66. for(Stringkey:rawParams.keySet()){
  67. //封装请求参数
  68. params.add(newBasicNameValuePair(key,rawParams.get(key)));
  69. }
  70. try{
  71. //设置请求参数
  72. post.setEntity(newUrlEncodedFormEntity(params,"GBK"));
  73. //发送POST请求
  74. httpResponse=httpClient.execute(post);
  75. //如果服务器成功的返回相应
  76. if(httpResponse.getStatusLine().getStatusCode()==200){
  77. //获取服务器响应的字符串
  78. result=EntityUtils.toString(httpResponse.getEntity());
  79. }
  80. }catch(ClientProtocolExceptione){
  81. e.printStackTrace();
  82. }catch(IOExceptione){
  83. e.printStackTrace();
  84. }
  85. returnresult;
  86. }
  87. }

3.开发Android应用程序,对JSON(或者其它格式数据交互对象)进行处理,获取需要的信息。

Android应用开发相对于已有的Web服务应用而言是独立的,可以将应用程序对服务器的请求和响应重新抽象一层,在已有的Web服务请求响应的控制层进行扩展和特定格式的数据信息封装。

本文出自 “野马红尘” 博客,谢绝转载!

更多相关文章

  1. 【23】Android 应用程序入口探究
  2. 如何降低android应用程序的耗电量
  3. Android设备FTP服务器搭建
  4. Android开发者如何搭建服务器
  5. android Java开发设计模式及在android中的应用解析

随机推荐

  1. Yahoo,Msn,Skype,QQ,阿里旺旺在线聊天链
  2. 如果条件按顺序,Mysql不能使用mysql
  3. 利用TPC-H为MYSQL生成数据
  4. 如何最好地处理重复日历事件的异常
  5. mysql中逗号分隔字段的更好替代方案
  6. Mysql--可用的 MySQL 产品和专业服务
  7. mysql5.6.23安装 步骤
  8. MySQL2 Ruby gem不会安装10.6
  9. Invalid property 'driver_class' of bea
  10. EntityFramework6连接MySql数据库 乱码问