本文共 21443 字,大约阅读时间需要 71 分钟。
<p>昨天我们只对Android接收网络数据进行了简单介绍,今天我们完成了Android数据存储网络部分的所有内容。在此我将对这非常重要的内容进行总结。 </p> <p>本篇日志是对Android与WEB应用服务之间进行数据交互的总结,下篇日志是一个经典而又让人十分好奇的Android多线程断点下载应用的总结。下面我们开始Android与网络数据的交互。</p> <p><strong>一、创建WEB应用服务</strong></p> <p>使用eclipse3.5创建一个动态WEB应用,使用Struts1处理用户请求。我们此应用添加一个DispatchAction,并为它添加四个方法创建用于处理Android以各种方式提交的请求。</p> <p><strong>1.</strong><strong>创建动态WEB工程</strong></p> <p>Project name:AndroidWebServer</p> <p>Target runtime:Apache Tomcat v6.0</p> <p>Dynamic web module version:2.5</p> <p>Configuration:Default Configuration for Apache Tomcat v6.0</p> <p><strong>2.</strong><strong>添加DispatchAction</strong></p> <p><strong>package</strong> com.changcheng.web.struts.actions;</p> <p><strong>import</strong> java.io.File;</p> <p><strong>import</strong> java.io.FileOutputStream;</p> <p><strong>import</strong> javax.servlet.http.HttpServletRequest;</p> <p><strong>import</strong> javax.servlet.http.HttpServletResponse;</p> <p><strong>import</strong> org.apache.struts.action.ActionForm;</p> <p><strong>import</strong> org.apache.struts.action.ActionForward;</p> <p><strong>import</strong> org.apache.struts.action.ActionMapping;</p> <p><strong>import</strong> org.apache.struts.actions.DispatchAction;</p> <p><strong>import</strong> com.changcheng.web.struts.forms.DataForm;</p> <p><strong>public</strong> <strong>class</strong> AndroidWebServer <strong>extends</strong> DispatchAction {</p> <p>// <span style="text-decoration: underline;">Andoird</span>以Get方式发送的请求</p> <p><strong>public</strong> ActionForward sendDataByGet(ActionMapping mapping, ActionForm form,</p> <p>HttpServletRequest request, HttpServletResponse response)</p> <p><strong>throws</strong> Exception {</p> <p>String name = request.getParameter("name");</p> <p>request.setAttribute("message", "Hello " + name);</p> <p><strong>return</strong> mapping.findForward("success");</p> <p>}</p> <p>// <span style="text-decoration: underline;">Andoird</span>以Post方式发送的请求</p> <p><strong>public</strong> ActionForward sendDataByPost(ActionMapping mapping, ActionForm form,</p> <p>HttpServletRequest request, HttpServletResponse response)</p> <p><strong>throws</strong> Exception {</p> <p>String name = request.getParameter("name");</p> <p>request.setAttribute("message", "Hello " + name);</p> <p><strong>return</strong> mapping.findForward("success");</p> <p>}</p> <p>// Andoird以表单方式发送的请求</p> <p><strong>public</strong> ActionForward sendDataByForm(ActionMapping mapping, ActionForm form,</p> <p>HttpServletRequest request, HttpServletResponse response)</p> <p><strong>throws</strong> Exception {</p> <p>DataForm formbean = (DataForm) form;</p> <p>System.<em>out</em>.println("StrData:" + formbean.getStrData());</p> <p>// 获取上传的文件</p> <p><strong>if</strong> (formbean.getFileData() != <strong>null</strong></p> <p>&& formbean.getFileData().getFileSize() > 0) {</p> <p>// 设置保存目录</p> <p>File dir = <strong>new</strong> File(request.getSession().getServletContext()</p> <p>.getRealPath("/images"));</p> <p><strong>if</strong> (!dir.exists())</p> <p>dir.mkdirs();</p> <p>// 保存文件</p> <p>FileOutputStream outStream = <strong>new</strong> FileOutputStream(<strong>new</strong> File(dir,</p> <p>formbean.getFileData().getFileName()));</p> <p>outStream.write(formbean.getFileData().getFileData());// 保存文件</p> <p>outStream.close();</p> <p>}</p> <p><strong>return</strong> <strong>null</strong>;</p> <p>}</p> <p>}</p> <p><strong>3.</strong><strong>向web.xml添加Struts1的ActionServlet</strong></p> <p></p> <p>struts</p> <p>org.apache.struts.action.ActionServlet</p> <p></p> <p>config</p> <p>/WEB-INF/struts-config.xml</p> <p></p> <p></p> <p></p> <p>struts</p> <p>*.do</p> <p></p> <p><strong>4.struts-config.xml</strong></p> <p><!--l version=<-->"1.0" encoding=<em>"UTF-8"</em>?></p> <p><!--CTYPE struts-config PUBLIC</d--> </p> <p>"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"</p> <p>"http://struts.apache.org/dtds/struts-config_1_3.dtd"></p> <p></p> <p></p> <p>"dataForm" type=<em>"com.changcheng.web.struts.forms.DataForm"</em> /></p> <p></p> <p></p> <p>"/server"</p> <p>type=<em>"com.changcheng.web.struts.actions.AndroidWebServer"</em> name=<em>"dataForm"</em></p> <p>scope=<em>"request"</em> parameter=<em>"method"</em>></p> <p>"success" path=<em>"/WEB-INF/pages/success.jsp"</em>/></p> <p></p> <p></p> <p></p> <p><strong>二、创建Android应用</strong></p> <p><strong>1.</strong><strong>创建Android工程</strong></p> <p>Project name:AndroidWebClient</p> <p>BuildTarget:Android2.1</p> <p>Application name:AndroidWEB应用客户端</p> <p>Package name:com.changcheng.web.client</p> <p>Create Activity:AndroidWebClient</p> <p>Min SDK Version:7</p> <p><strong>2.AndroidManifest.xml</strong></p> <p><!--l version=<-->"1.0" encoding=<em>"utf-8"</em>?></p> <p>"http://schemas.android.com/apk/res/android"</p> <p>package=<em>"com.changcheng.web.client"</em> android:versionCode=<em>"1"</em></p> <p>android:versionName=<em>"1.0"</em>></p> <p>"@drawable/icon" android:label=<em>"@string/app_name"</em>></p> <p><!-- 单元测试 --></p> <p>"android.test.runner" /></p> <p>".AndroidWebClient" android:label=<em>"@string/app_name"</em>></p> <p></p> <p>"android.intent.action.MAIN" /></p> <p>"android.intent.category.LAUNCHER" /></p> <p></p> <p></p> <p></p> <p>"7" /></p> <p><!-- 访问internet权限 --></p> <p>"android.permission.INTERNET" /></p> <p><!-- 在SDCard中创建与删除文件权限 --></p> <p>"android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /></p> <p><!-- 往SDCard写入数据权限 --></p> <p>"android.permission.WRITE_EXTERNAL_STORAGE" /></p> <p><!-- 单元测试 --></p> <p>"android.test.InstrumentationTestRunner"</p> <p>android:targetPackage=<em>"com.changcheng.web.client"</em> android:label=<em>"Tests for My App"</em> /></p> <p></p> <p>Android应用要访问Internet需要添加权限。</p> <p><strong>3.ClientService</strong><strong>类</strong></p> <p><strong>package</strong> com.changcheng.web.client.service;</p> <p><strong>import</strong> java.io.ByteArrayOutputStream;</p> <p><strong>import</strong> java.io.DataOutputStream;</p> <p><strong>import</strong> java.io.File;</p> <p><strong>import</strong> java.io.FileInputStream;</p> <p><strong>import</strong> java.io.InputStream;</p> <p><strong>import</strong> java.net.HttpURLConnection;</p> <p><strong>import</strong> java.net.URL;</p> <p><strong>import</strong> java.util.HashMap;</p> <p><strong>import</strong> java.util.Map;</p> <p><strong>import</strong> android.os.Environment;</p> <p><strong>import</strong> android.util.Log;</p> <p><strong>public</strong> <strong>class</strong> ClientService {</p> <p><strong>private</strong> <strong>static</strong> <strong>final</strong> String <em>TAG</em> = "ClientService";</p> <p>// 以get方式发送请求</p> <p><strong>public</strong> <strong>static</strong> <strong>void</strong> sendDataToServerByGet() <strong>throws</strong> Exception {</p> <p>// 主机地址不可以设置为localhost或127.0.0.1,必须是本机或其他机器所在Internet网或局域网地址。</p> <p>String path = "http://192.168.0.2:8080/AndroidWebServer/server.do?"</p> <p>+ "method=sendDataByGet&name=changcheng";</p> <p>URL url = <strong>new</strong> URL(path);</p> <p>HttpURLConnection conn = (HttpURLConnection) url.openConnection();</p> <p>conn.setConnectTimeout(6 * 1000);</p> <p>// 请求成功</p> <p><strong>if</strong> (conn.getResponseCode() == 200) {</p> <p>// 获取服务器返回的数据</p> <p><strong>byte</strong>[] data = <em>readStream</em>(conn.getInputStream());</p> <p>Log.<em>i</em>(<em>TAG</em>, <strong>new</strong> String(data, "UTF-8"));</p> <p>}</p> <p>}</p> <p>// 以Post方式发送请求,面向HTTP协议编程</p> <p><strong>public</strong> <strong>static</strong> <strong>void</strong> sendDataTOserverByPost() <strong>throws</strong> Exception {</p> <p>String path = "http://192.168.0.2:8080/AndroidWebServer/server.do";</p> <p>String params = "method=sendDataByPost&name=tingting";// 请求参数</p> <p><strong>byte</strong>[] data = params.getBytes();</p> <p>URL url = <strong>new</strong> URL(path);</p> <p>HttpURLConnection conn = (HttpURLConnection) url.openConnection();</p> <p>conn.setConnectTimeout(6 * 1000);</p> <p>conn.setDoOutput(<strong>true</strong>);// 发送POST请求必须设置允许输出</p> <p>conn.setUseCaches(<strong>false</strong>);// 不使用Cache</p> <p>conn.setRequestMethod("POST");</p> <p>conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接</p> <p>conn.setRequestProperty("Charset", "UTF-8");</p> <p>conn.setRequestProperty("Content-Length", String.<em>valueOf</em>(data.length));</p> <p>conn.setRequestProperty("Content-Type",</p> <p>"application/x-www-form-urlencoded");</p> <p>DataOutputStream outStream = <strong>new</strong> DataOutputStream(conn</p> <p>.getOutputStream());</p> <p>outStream.write(data);// 以内容实体方式发送请求参数</p> <p>outStream.flush();</p> <p>outStream.close();</p> <p>// 请求成功</p> <p><strong>if</strong> (conn.getResponseCode() == 200) {</p> <p>// 获取服务器返回的数据</p> <p><strong>byte</strong>[] html = <em>readStream</em>(conn.getInputStream());</p> <p>Log.<em>i</em>(<em>TAG</em>, <strong>new</strong> String(html, "UTF-8"));</p> <p>}</p> <p>}</p> <p>// 以表单方式发送请求</p> <p><strong>public</strong> <strong>static</strong> <strong>void</strong> sendDataToServerByForm() <strong>throws</strong> Exception {</p> <p>Map params = <strong>new</strong> HashMap();</p> <p>params.put("method", "sendDataByForm");</p> <p>params.put("strData", "字符串数据");</p> <p>// 获取SDCard中的good.jpg</p> <p>File file = <strong>new</strong> File(Environment.<em>getExternalStorageDirectory</em>(),</p> <p>"app_Goog_Android_w.png");</p> <p>FormFile fileData = <strong>new</strong> FormFile("app_Goog_Android_w.png", <strong>new</strong> FileInputStream(file),</p> <p>"fileData", "application/octet-stream");</p> <p>HttpRequester.<em>post</em>(</p> <p>"http://192.168.0.2:8080/AndroidWebServer/server.do", params,</p> <p>fileData);</p> <p>}</p> <p>// 获取输入流数据</p> <p><strong>private</strong> <strong>static</strong> <strong>byte</strong>[] readStream(InputStream inStream) <strong>throws</strong> Exception {</p> <p><strong>byte</strong>[] buffer = <strong>new</strong> <strong>byte</strong>[1024];</p> <p><strong>int</strong> len = -1;</p> <p>ByteArrayOutputStream outStream = <strong>new</strong> ByteArrayOutputStream();</p> <p><strong>while</strong> ((len = inStream.read(buffer)) != -1) {</p> <p>outStream.write(buffer, 0, len);</p> <p>}</p> <p><strong>byte</strong>[] data = outStream.toByteArray();</p> <p>outStream.close();</p> <p>inStream.close();</p> <p><strong>return</strong> data;</p> <p>}</p> <p>}</p> <p>其中使用到的FormFile类:</p> <p><strong>package</strong> com.changcheng.web.client.service;</p> <p><strong>import</strong> java.io.InputStream;</p> <p>/**</p> <p>* 上传文件</p> <p>*/</p> <p><strong>public</strong> <strong>class</strong> FormFile {</p> <p>/* 上传文件的数据 */</p> <p><strong>private</strong> <strong>byte</strong>[] data;</p> <p><strong>private</strong> InputStream inStream;</p> <p>/* 文件名称 */</p> <p><strong>private</strong> String filname;</p> <p>/* 表单字段名称*/</p> <p><strong>private</strong> String formname;</p> <p>/* 内容类型 */</p> <p><strong>private</strong> String contentType = "application/octet-stream";</p> <p><strong>public</strong> FormFile(String filname, <strong>byte</strong>[] data, String formname, String contentType) {</p> <p><strong>this</strong>.data = data;</p> <p><strong>this</strong>.filname = filname;</p> <p><strong>this</strong>.formname = formname;</p> <p><strong>if</strong>(contentType!=<strong>null</strong>) <strong>this</strong>.contentType = contentType;</p> <p>}</p> <p><strong>public</strong> FormFile(String filname, InputStream inStream, String formname, String contentType) {</p> <p><strong>this</strong>.filname = filname;</p> <p><strong>this</strong>.formname = formname;</p> <p><strong>this</strong>.inStream = inStream;</p> <p><strong>if</strong>(contentType!=<strong>null</strong>) <strong>this</strong>.contentType = contentType;</p> <p>}</p> <p><strong>public</strong> InputStream getInStream() {</p> <p><strong>return</strong> inStream;</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> setInStream(InputStream inStream) {</p> <p><strong>this</strong>.inStream = inStream;</p> <p>}</p> <p><strong>public</strong> <strong>byte</strong>[] getData() {</p> <p><strong>return</strong> data;</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> setData(<strong>byte</strong>[] data) {</p> <p><strong>this</strong>.data = data;</p> <p>}</p> <p><strong>public</strong> String getFilname() {</p> <p><strong>return</strong> filname;</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> setFilname(String filname) {</p> <p><strong>this</strong>.filname = filname;</p> <p>}</p> <p><strong>public</strong> String getFormname() {</p> <p><strong>return</strong> formname;</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> setFormname(String formname) {</p> <p><strong>this</strong>.formname = formname;</p> <p>}</p> <p><strong>public</strong> String getContentType() {</p> <p><strong>return</strong> contentType;</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> setContentType(String contentType) {</p> <p><strong>this</strong>.contentType = contentType;</p> <p>}</p> <p>}</p> <p>其中使用到的HttpRequester类:</p> <p><strong>package</strong> com.changcheng.web.client.service;</p> <p><strong>import</strong> java.io.DataOutputStream;</p> <p><strong>import</strong> java.io.InputStream;</p> <p><strong>import</strong> java.net.HttpURLConnection;</p> <p><strong>import</strong> java.net.URL;</p> <p><strong>import</strong> java.util.ArrayList;</p> <p><strong>import</strong> java.util.List;</p> <p><strong>import</strong> java.util.Map;</p> <p><strong>import</strong> org.apache.http.HttpResponse;</p> <p><strong>import</strong> org.apache.http.NameValuePair;</p> <p><strong>import</strong> org.apache.http.client.entity.UrlEncodedFormEntity;</p> <p><strong>import</strong> org.apache.http.client.methods.HttpPost;</p> <p><strong>import</strong> org.apache.http.impl.client.DefaultHttpClient;</p> <p><strong>import</strong> org.apache.http.message.BasicNameValuePair;</p> <p><strong>import</strong> org.apache.http.protocol.HTTP;</p> <p><strong>import</strong> org.apache.http.util.EntityUtils;</p> <p><strong>import</strong> android.util.Log;</p> <p>/**</p> <p>* <span style="text-decoration: underline;">http</span>请求发送器</p> <p>*/</p> <p><strong>public</strong> <strong>class</strong> HttpRequester {</p> <p>/**</p> <p>* 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能:</p> <p>* </p> <div><form action="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="application/x-www-form-urlencoded" method="post">enctype="<span style="text-decoration: underline;">multipart</span>/form-data"></form></div> <form id="[object]" action="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="application/x-www-form-urlencoded" method="post"> <p><input name="name" type="text"></p> <p><input name="id" type="text"></p> <p><input name="<u>imagefile</u>" type="file"></p> <p><input name="<u>zip</u>" type="file"></p> <p></p> </form> <p>* <strong>@param</strong> actionUrl 上传路径(注:避免使用<span style="text-decoration: underline;">localhost</span>或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)</p> <p>* <strong>@param</strong> params 请求参数 key为参数名,value为参数值</p> <p>* <strong>@param</strong> file 上传文件</p> <p>*/</p> <p><strong>public</strong> <strong>static</strong> String post(String actionUrl, Map params, FormFile[] files) {</p> <p><strong>try</strong> {</p> <p>String BOUNDARY = "---------7d 4a6d158c9"; //数据分隔线</p> <p>String MULTIPART_FORM_DATA = "multipart/form-data";</p> <p>URL url = <strong>new</strong> URL(actionUrl);</p> <p>HttpURLConnection conn = (HttpURLConnection) url.openConnection();</p> <p>conn.setConnectTimeout(6* 1000);</p> <p>conn.setDoInput(<strong>true</strong>);//允许输入</p> <p>conn.setDoOutput(<strong>true</strong>);//允许输出</p> <p>conn.setUseCaches(<strong>false</strong>);//不使用Cache</p> <p>conn.setRequestMethod("POST");</p> <p>conn.setRequestProperty("Connection", "Keep-Alive");</p> <p>conn.setRequestProperty("Charset", "UTF-8");</p> <p>conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);</p> <p>StringBuilder sb = <strong>new</strong> StringBuilder();</p> <p><strong>for</strong> (Map.Entry entry : params.entrySet()) {//构建表单字段内容</p> <p>sb.append("--");</p> <p>sb.append(BOUNDARY);</p> <p>sb.append("/r/n");</p> <p>sb.append("Content-Disposition: form-data; name=/""+ entry.getKey() + "/"/r/n/r/n");</p> <p>sb.append(entry.getValue());</p> <p>sb.append("/r/n");</p> <p>}</p> <p>DataOutputStream outStream = <strong>new</strong> DataOutputStream(conn.getOutputStream());</p> <p>outStream.write(sb.toString().getBytes());//发送表单字段数据</p> <p><strong>for</strong>(FormFile file : files){//发送文件数据</p> <p>StringBuilder split = <strong>new</strong> StringBuilder();</p> <p>split.append("--");</p> <p>split.append(BOUNDARY);</p> <p>split.append("/r/n");</p> <p>split.append("Content-Disposition: form-data;name=/""+ file.getFormname()+"/";filename=/""+ file.getFilname() + "/"/r/n");</p> <p>split.append("Content-Type: "+ file.getContentType()+"/r/n/r/n");</p> <p>outStream.write(split.toString().getBytes());</p> <p><strong>if</strong>(file.getInStream()!=<strong>null</strong>){</p> <p><strong>byte</strong>[] buffer = <strong>new</strong> <strong>byte</strong>[1024];</p> <p><strong>int</strong> len = 0;</p> <p><strong>while</strong>((len = file.getInStream().read(buffer))!=-1){</p> <p>outStream.write(buffer, 0, len);</p> <p>}</p> <p>file.getInStream().close();</p> <p>}<strong>else</strong>{</p> <p>outStream.write(file.getData(), 0, file.getData().length);</p> <p>}</p> <p>outStream.write("/r/n".getBytes());</p> <p>}</p> <p><strong>byte</strong>[] end_data = ("--" + BOUNDARY + "--/r/n").getBytes();//数据结束标志 </p> <p>outStream.write(end_data);</p> <p>outStream.flush();</p> <p><strong>int</strong> cah = conn.getResponseCode();</p> <p><strong>if</strong> (cah != 200) <strong>throw</strong> <strong>new</strong> RuntimeException("请求url失败");</p> <p>InputStream is = conn.getInputStream();</p> <p><strong>int</strong> ch;</p> <p>StringBuilder b = <strong>new</strong> StringBuilder();</p> <p><strong>while</strong>( (ch = is.read()) != -1 ){</p> <p>b.append((<strong>char</strong>)ch);</p> <p>}</p> <p>Log.<em>i</em>("ItcastHttpPost", b.toString());</p> <p>outStream.close();</p> <p>conn.disconnect();</p> <p><strong>return</strong> b.toString();</p> <p>} <strong>catch</strong> (Exception e) {</p> <p><strong>throw</strong> <strong>new</strong> RuntimeException(e);</p> <p>}</p> <p>}</p> <p>/**</p> <p>* 提交数据到服务器</p> <p>* <strong>@param</strong> actionUrl 上传路径(注:避免使用<span style="text-decoration: underline;">localhost</span>或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)</p> <p>* <strong>@param</strong> params 请求参数 key为参数名,value为参数值</p> <p>* <strong>@param</strong> file 上传文件</p> <p>*/</p> <p><strong>public</strong> <strong>static</strong> String post(String actionUrl, Map params, FormFile file) {</p> <p><strong>return</strong> <em>post</em>(actionUrl, params, <strong>new</strong> FormFile[]{file});</p> <p>}</p> <p>/**</p> <p>* 提交数据到服务器</p> <p>* <strong>@param</strong> actionUrl 上传路径(注:避免使用<span style="text-decoration: underline;">localhost</span>或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试)</p> <p>* <strong>@param</strong> params 请求参数 key为参数名,value为参数值</p> <p>*/</p> <p><strong>public</strong> <strong>static</strong> String post(String actionUrl, Map params) {</p> <p>HttpPost httpPost = <strong>new</strong> HttpPost(actionUrl);</p> <p>List list = <strong>new</strong> ArrayList();</p> <p><strong>for</strong> (Map.Entry entry : params.entrySet()) {//构建表单字段内容</p> <p>list.add(<strong>new</strong> BasicNameValuePair(entry.getKey(), entry.getValue()));</p> <p>}</p> <p><strong>try</strong> {</p> <p>httpPost.setEntity(<strong>new</strong> UrlEncodedFormEntity(list, HTTP.<em>UTF_8</em>));</p> <p>HttpResponse httpResponse = <strong>new</strong> DefaultHttpClient().execute(httpPost);</p> <p><strong>if</strong>(httpResponse.getStatusLine().getStatusCode() == 200){</p> <p><strong>return</strong> EntityUtils.<em>toString</em>(httpResponse.getEntity());</p> <p>}</p> <p>} <strong>catch</strong> (Exception e) {</p> <p><strong>throw</strong> <strong>new</strong> RuntimeException(e);</p> <p>}</p> <p><strong>return</strong> <strong>null</strong>;</p> <p>}</p> <p>}</p> <p>我们最好对HTTP协议有深入的了解,这样在编写简单数据交互应用时直接面向HTTP协议编程可以提高运行速度并减少资源的占用。</p> <p>我们在最后一个方法中使用到的HttpPost类,是Apache开源组织提供的httpcomponents-client-4.0.1包。httpcomponents-client-4.0.1可以实现浏览器的大部分功能,但如果我们能不使用它就尽量不使用它,因为这会造成对手机硬件资源的占用,从而减慢应用程序的运行速度。</p> <p><strong>4.</strong><strong>测试类</strong></p> <p><strong>package</strong> com.changcheng.web.client.test;</p> <p><strong>import</strong> com.changcheng.web.client.service.ClientService;</p> <p><strong>import</strong> android.test.AndroidTestCase;</p> <p><strong>public</strong> <strong>class</strong> TestAndroidClientService <strong>extends</strong> AndroidTestCase {</p> <p><strong>public</strong> <strong>void</strong> testSendDataToServerByGet() <strong>throws</strong> Throwable {</p> <p>ClientService.<em>sendDataToServerByGet</em>();</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> testSendDataToServerByPost() <strong>throws</strong> Throwable {</p> <p>ClientService.<em>sendDataTOserverByPost</em>();</p> <p>}</p> <p><strong>public</strong> <strong>void</strong> testSendDataToServerByForm() <strong>throws</strong> Throwable {</p> <p>ClientService.<em>sendDataToServerByForm</em>();</p> <p>}</p> <p>}</p> <p><strong>5.</strong><strong>运行</strong></p> <p>首先启动AndroidWebService应用程序,然后运行测试方法,查看运行结果。</p>转载地址:http://kcaji.baihongyu.com/