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. Android中不同应用间实现SharedPreferences数据共享
  2. 关于Android(安卓)Studio3.2新建项目Android(安卓)resource link
  3. Android(安卓)- Manifest 文件 详解
  4. Android之应用程序基础
  5. Android四大组件的理解
  6. Android使用Retrofit进行网络请求
  7. Android官方入门文档[1]创建一个Android项目
  8. 第三章 Android程序设计基础
  9. 第一章 andriod studio 安装与环境搭建

随机推荐

  1. php将图片以二进制形式保存到mysql数据库
  2. 为什么插入忽略递增的auto_increment主键
  3. 解决The&#39;InnoDB&#39;feature is disa
  4. mysql常识以及存储引擎,锁和事务
  5. 05-mysql中的查询(第一章)
  6. Active Record或mysql中的批次计数
  7. Innode表空间碎片优化
  8. Win2003+IIS6.0+PHP+MYSQL+ASP+ASP.NET全
  9. VS2015 使用Mysql-connector/c++ 链接数
  10. mysql数据库监控利器lepus天兔工具安装和