Android图片压缩技巧

最后更新于:2022-04-01 09:32:33

请尊重他人的劳动成果,转载请注明出处:[Android图片压缩技巧](http://blog.csdn.net/fengyuzhengfan/article/details/41759835) [http://blog.csdn.net/fengyuzhengfan/article/details/41759835](http://blog.csdn.net/fengyuzhengfan/article/details/41759835) 当需要将Android客户端的图片上传到服务器时,往往需要将图片进行压缩,关于图片的压缩方法,小编分享几种常用的方式: ## 第一种方式:裁切以达到压缩的目的 我曾在《[Android开发之裁剪照片](http://blog.csdn.net/fengyuzhengfan/article/details/39940587)》一文中详细介绍过如何裁切照片,感兴趣的朋友可以去看一下。 ## 第二种方式:将图片进行降质处理(即降低图片的质量)以达到压缩的目的 这种方式也是比较常用的方式,下面就为大家介绍如何对图片进行降质: 将图片降质我们可以使用Bitmap的这个方法:boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream) 其中,参数format表示压缩后的格式,quality压缩后的图片质量(0表示最低,100表示不压缩),stream表示要将压缩后的图片保存到的输出流。 下面是详细代码: ~~~ /** * 多线程压缩图片的质量 * @author JPH * @param bitmap 内存中的图片 * @param imgPath 图片的保存路径 * @date 2014-12-5下午11:30:43 */ public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){ new Thread(new Runnable() {//开启多线程进行压缩处理 @Override public void run() { // TODO Auto-generated method stub ByteArrayOutputStream baos = new ByteArrayOutputStream(); options = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小) while (baos.toByteArray().length / 1024 > 100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即让下一次的写入覆盖之前的内容 options -= 10;//图片质量每次减少10 if(options<0)options=0;//如果图片质量小于10,则将图片的质量压缩到最小值 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中 if(options==0)break;//如果图片的质量已降到最低则,不再进行压缩 } try { FileOutputStream fos = new FileOutputStream(new File(imgPath));//将压缩后的图片保存的本地上指定路径中 fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } ~~~ 方法解析: 由于此方法中包含I/O操作和递归调用比较耗时所以我采用了多线程去处理。 ## 第三种方式:按比例缩小图片的像素以达到压缩的目的 此种方法主要是使用android.graphics.BitmapFactory.Options.Options()方法将图片以指定的采用率加载到内存然后输出到本地以达到压缩像素的目的。 详细代码: ~~~ /** * 按比例缩小图片的像素以达到压缩的目的 * @author JPH * @param imgPath * @date 2014-12-5下午11:30:59 */ public static void compressImageByPixel(String imgPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;//只读边,不读内容 Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts); newOpts.inJustDecodeBounds = false; int width = newOpts.outWidth; int height = newOpts.outHeight; float maxSize = 1000f;//默认1000px int be = 1; if (width > height && width > maxSize) {//缩放比,用高或者宽其中较大的一个数据进行计算 be = (int) (newOpts.outWidth / maxSize); } else if (width < height && height > maxSize) { be = (int) (newOpts.outHeight / maxSize); } be++; newOpts.inSampleSize = be;//设置采样率 newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设 newOpts.inPurgeable = true;// 同时设置才会有效 newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收 bitmap = BitmapFactory.decodeFile(imgPath, newOpts); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); try { FileOutputStream fos = new FileOutputStream(new File(imgPath)); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } ~~~ ## 第四种方式:将图片先按比例压缩然后再降质 此种方式主要结合第二种和第三种方法以下是详细代码: ~~~ /** * 多线程压缩图片的质量 * @author JPH * @param bitmap 内存中的图片 * @param imgPath 图片的保存路径 * @date 2014-12-5下午11:30:43 */ public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){ new Thread(new Runnable() {//开启多线程进行压缩处理 @Override public void run() { // TODO Auto-generated method stub ByteArrayOutputStream baos = new ByteArrayOutputStream(); options = 100; bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小) while (baos.toByteArray().length / 1024 > 100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即让下一次的写入覆盖之前的内容 options -= 10;//图片质量每次减少10 if(options<0)options=0;//如果图片质量小于10,则将图片的质量压缩到最小值 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中 if(options==0)break;//如果图片的质量已降到最低则,不再进行压缩 } try { FileOutputStream fos = new FileOutputStream(new File(imgPath));//将压缩后的图片保存的本地上指定路径中 fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } /** * 按比例缩小图片的像素以达到压缩的目的 * @author JPH * @param imgPath * @date 2014-12-5下午11:30:59 */ public static void compressImageByPixel(String imgPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;//只读边,不读内容 Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts); newOpts.inJustDecodeBounds = false; int width = newOpts.outWidth; int height = newOpts.outHeight; float maxSize = 1000f;//默认1000px int be = 1; if (width > height && width > maxSize) {//缩放比,用高或者宽其中较大的一个数据进行计算 be = (int) (newOpts.outWidth / maxSize); } else if (width < height && height > maxSize) { be = (int) (newOpts.outHeight / maxSize); } be++; newOpts.inSampleSize = be;//设置采样率 newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设 newOpts.inPurgeable = true;// 同时设置才会有效 newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收 bitmap = BitmapFactory.decodeFile(imgPath, newOpts); compressImageByQuality(bitmap,imgPath);//压缩好比例大小后再进行质量压缩 } ~~~
';

Android网络编程之使用HttpClient批量上传文件(一)

最后更新于:2022-04-01 09:32:31

请尊重他人的劳动成果,转载请注明出处:[](http://blog.csdn.net/fengyuzhengfan/article/details/39941851)[Android网络编程之使用HttpClient批量上传文件](http://blog.csdn.net/fengyuzhengfan/article/details/39941851) 我曾在《[Android网络编程之使用HTTP访问网络资源](http://blog.csdn.net/fengyuzhengfan/article/details/38919259)》一文中介绍过HttpCient的使用,这里就不在累述了,感兴趣的朋友可以去看一下。在这里主要介绍如何通过HttpClient实现文件上传。 ## 1.预备知识: 在HttpCient4.3之前上传文件主要使用MultipartEntity这个类,但现在这个类已经不在推荐使用了。随之替代它的类是MultipartEntityBuilder。 ### 下面让我们了解一下MultipartEntityBuilder类: MultipartEntityBuilder这个类主要用于创建HttpEntity。它的主要方法有: <table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border:solid #8EAADB 1.0pt; border-left:none; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span style="font-size:9.0pt; font-family:宋体; color:#353833">修饰符和类型</span><span lang="EN-US" style="font-size:9.0pt; font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border-top:solid #8EAADB 1.0pt; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:none; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span style="font-size:9.0pt; font-family:宋体; color:#353833">方法和描述</span><span lang="EN-US" style="font-size:9.0pt; font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addBinaryBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, byte[] b)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">将字节数组以二进制的形式添加数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addBinaryBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, byte[] b, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">ContentType</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> contentType, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> filename)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">将字节数组以二进制的形式添加数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addBinaryBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">File</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> file)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">将文件以二进制的形式添加数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addBinaryBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">File</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> file, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">ContentType</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> contentType, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> filename)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">将文件以二进制的形式添加数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addBinaryBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">InputStream</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> stream)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addBinaryBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">InputStream</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> stream, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">ContentType</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> contentType, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> filename)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">将输入流以二进制的形式添加数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addPart</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">ContentBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> contentBody)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">添加</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">ContentBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> </span><span style="font-family:宋体; color:#353833">类型的数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addTextBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> text)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">添加文本数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">addTextBody</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> name, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> text, </span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">ContentType</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> contentType)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">以指定的内容类型添加文本数据。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">HttpEntity</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">build</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">()</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">创建一个</span><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">HttpEntity</span></span><span style="font-family:宋体; color:#4C6B87">。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US" style="font-family:宋体; color:#353833">static </span><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">create</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">()</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">创建一个</span><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span style="font-family:宋体; color:#4C6B87">对象。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">setBoundary</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">String</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> boundary)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">设置边界。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">setCharset</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">Charset</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> charset)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">设置请求的编码格式。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">setLaxMode</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">()</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">setMode</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">(</span><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">HttpMultipartMode</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833"> mode)</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p><p class="MsoNormal" align="left" style="text-align:left"><span style="font-family:宋体; color:#353833">设置模式。</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></p></td></tr><tr style=""><td width="132" nowrap="nowrap" valign="top" style="width:99.25pt; border-top:none; border-left:none; border-bottom:solid #8EAADB 1.0pt; border-right:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US"><span style="font-family:宋体; color:#4C6B87; text-decoration:none">MultipartEntityBuilder</span></span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"/></strong></p></td><td width="425" valign="top" style="width:318.95pt; border:none; border-bottom:solid #8EAADB 1.0pt; background:#D9E2F3; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left"><span lang="EN-US"><strong><span style="font-family:宋体; color:#4C6B87; text-decoration:none">setStrictMode</span></strong></span><span lang="EN-US" style="font-family:宋体; color:#353833">()</span><span lang="EN-US" style="font-family:'Arial','sans-serif'; color:#353833"> </span></p></td></tr></tbody></table> 主要方法说明: **addBinaryBody**、**addPart**、**addTextBody**方法用于添加要上传的数据,从上面的表格中可以发现用于添加数据的方法,都是**key-value**类型。所以在服务器端我们可以通过request.getPart("keyname")方式获取对应key的数据。也可以通过request.getParts()方式获取客户端通过以上三种方法提交所有数据。 1.通过**addBinaryBody**方法直接可以添加**File、InputStream、byte[]**类型的数据。 2.通过**addPart**方法只能添加**ContentBody**类型的数据,在org.apache.http.entity.mime.content包中已经提供了**String**、**File**以及**InputStream**对应的**ContentBody**类型的子类,如FileBody、InputStreamBody、StringBody,通过这些类我们可以将**String**、File以及**InputStream**类型的数据转换成**ContentBody**类型的数据。 3.通过**addTextBody**方法我们可以很方便的添加文本数据。 ## 2.通过HttpCient上传文件 Android端需要添加[httpcore-4.3.2.jar](http://download.csdn.net/detail/fengyuzhengfan/8018277)、[httpmime-4.3.5.jar](http://download.csdn.net/detail/fengyuzhengfan/8018281)两个包。两个包缺一不可。 在这里我用的是最新版的**HttpCient**,大家可以从[http://hc.apache.org/downloads.cgi](http://download.csdn.net/detail/fengyuzhengfan/8018289)上下载所需要的jar包,如果上面的网站打不开,大家也不用担心,我已经将项目中所需要的jar包上传到CSDN上《[httpcomponents-client-4.3.5-bin.zip](http://download.csdn.net/detail/fengyuzhengfan/8018289)》需要的朋友可以去下载。 ### Android端项目核心代码: ~~~ HttpClient client=new DefaultHttpClient();// 开启一个客户端 HTTP 请求 HttpPost post = new HttpPost(url);//创建 HTTP POST 请求 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // builder.setCharset(Charset.forName("uft-8"));//设置请求的编码格式 builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式 int count=0; for (File file:files) { // FileBody fileBody = new FileBody(file);//把文件转换成流对象FileBody // builder.addPart("file"+count, fileBody); builder.addBinaryBody("file"+count, file); count++; } builder.addTextBody("method", params.get("method"));//设置请求参数 builder.addTextBody("fileTypes", params.get("fileTypes"));//设置请求参数 HttpEntity entity = builder.build();// 生成 HTTP POST 实体 post.setEntity(entity);//设置请求参数 HttpResponse response = client.execute(post);// 发起请求 并返回请求的响应 if (response.getStatusLine().getStatusCode()==200) { return true; } return false; ~~~ 代码分析:  上面代码主要实现了多文件上传,为了方便服务器端保存文件,上面代码设置了名称为fileTypes的参数,fileTypes是由上传的文件类型名拼接成的字符串,如”.jpg.png.docx“; 服务器端可以通过获取名为fileTypes的参数,然后将其拆分成字符数组,即可得到要保存文件的类型。 ### 服务器端项目核心代码: 服务器段主要用到Servlet3.0的API,主要用到的方法有: 1.request.getParameter("");//获取客户端通过addTextBody方法添加的String类型的数据。 2.request.getPart("");//获取客户端通过**addBinaryBody**、**addPart**、**addTextBody**方法添加的指定数据,返回Part类型的对象。 3.request.getParts();//获取客户端通过**addBinaryBody**、**addPart**、**addTextBody**方法添加的所有数据,返回**Collection<Part>**类型的对象。 4.part.getName();//获取上传文件的名称即上传时指定的key。 5.part.getSize()//获取上传文件的大小单位为字节。 ~~~ String fileTypes=request.getParameter("fileTypes");//获取客户端上传的所有文件类型 String[]typeArray=fileTypes.substring(1).split("\\.");//将文件类型字符串拆分成String数组 try { Iterator<Part>iterator=request.getParts().iterator(); int count=0; while (iterator.hasNext()) {//遍历客户端上传的所有文件 if (count>=typeArray.length)break;//如果超出文件类型数组的大小则跳出循环 Part part = (Part) iterator.next(); // System.out.println("part.getSize()"+part.getSize());//获取上传文件的大小 // System.out.println("part.getName()"+part.getName());//获取上传文件的名及添加数据时的key名 File file=new File("E:\\upload\\"+count+"."+typeArray[count++]); InputStream inputStream=part.getInputStream(); FileOutputStream fos=new FileOutputStream(file); byte[]buffer=new byte[1024]; int len=0; while ((len=inputStream.read(buffer))!=-1) { fos.write(buffer,0, len); } inputStream.close(); fos.close(); } }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } ~~~ 代码分析: 服务器端是通过Servlet实现的,通过调用request.getParameter("fileTypes")方法来获取客户端上传的所有文件类型,然后将文件类型字符串拆分成String数组。通过request.getParts()方法取出客户端通过**addBinaryBody**、**addPart**、**addTextBody**上传的所有数据,然后遍历数据集合即可进行文件的保存。 由于事先和客户端协定,添加上传文件的顺序在添加请求参数之前,所以可以根据拆分出的文件类型数组的长度判断出客户端上传文件的个数,因此当上面代码遍历超出了类型数组的长度时程序跳出循环,不再进行文件的保存,因为下面的Part都是些参数,而不是要保存的文件了。 ## 程序运行效果图: ![Android使用HttpClient上传数据](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3969ce9.gif) ## 3.完成项目代码: MainActivity.java ~~~ package com.jph.ufh.activity; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.jph.ufh.R; import com.jph.ufh.service.UploadService; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Toast; /** * 通过httpClient批量上传文件 * @author jph * Date:2014.10.09 */ public class MainActivity extends Activity { private ArrayList<File>files; private Map<String, String>params; Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case UploadService.UPLOAD_SUCCESS: Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_LONG).show(); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); files=new ArrayList<File>(); params=new HashMap<String, String>(); } public void upload(View v) { files.clear(); params.clear(); File file=new File(Environment.getExternalStorageDirectory(),"kaola.jpg"); File file2=new File(Environment.getExternalStorageDirectory(),"test.docx"); File file3=new File(Environment.getExternalStorageDirectory(),"test.jpg"); files.add(file); files.add(file2); files.add(file3); StringBuffer sbFileTypes=new StringBuffer(); for (File tempFile:files) { String fileName=tempFile.getName(); sbFileTypes.append(getFileType(fileName)); } params.put("fileTypes",sbFileTypes.toString()); params.put("method", "upload"); UploadService uploadService=new UploadService(mHandler); uploadService.uploadFileToServer(params, files); } /** * 获取文件的类型 * @param fileName :文件名 * @return 文件类型 */ private String getFileType(String fileName) { // TODO Auto-generated method stub return fileName.substring(fileName.lastIndexOf("."), fileName.length()); } } ~~~ **UploadService.java** ~~~ package com.jph.ufh.service; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.DefaultHttpClient; import android.os.Handler; /** * 采用HttpClient上传文件,支持多文件上传 * @author jph * Date:2014.10.09 */ public class UploadService { private static String url="http://10.219.57.16:8080/ServerForUpload/ServletForUpload"; // private static String url="http://10.110.6.58:8080/ServerForUpload/ServletForUpload"; public static final int UPLOAD_SUCCESS=0x123; public static final int UPLOAD_FAIL=0x124; private Handler handler; public UploadService(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * @param params 请求参数,包括请求的的方法参数method如:“upload”, * 请求上传的文件类型fileTypes如:“.jpg.png.docx” * @param files 要上传的文件集合 */ public void uploadFileToServer(final Map<String, String> params, final ArrayList<File>files) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { if (uploadFiles(url,params,files)) { handler.sendEmptyMessage(UPLOAD_SUCCESS);//通知主线程数据发送成功 }else { //将数据发送给服务器失败 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } /** * @param url servlet的地址 * @param params 要传递的参数 * @param files 要上传的文件 * @return true if upload success else false * @throws ClientProtocolException * @throws IOException */ private boolean uploadFiles(String url,Map<String, String>params,ArrayList<File>files) throws ClientProtocolException, IOException { HttpClient client=new DefaultHttpClient();// 开启一个客户端 HTTP 请求 HttpPost post = new HttpPost(url);//创建 HTTP POST 请求 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // builder.setCharset(Charset.forName("uft-8"));//设置请求的编码格式 builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式 int count=0; for (File file:files) { // FileBody fileBody = new FileBody(file);//把文件转换成流对象FileBody // builder.addPart("file"+count, fileBody); builder.addBinaryBody("file"+count, file); count++; } builder.addTextBody("method", params.get("method"));//设置请求参数 builder.addTextBody("fileTypes", params.get("fileTypes"));//设置请求参数 HttpEntity entity = builder.build();// 生成 HTTP POST 实体 post.setEntity(entity);//设置请求参数 HttpResponse response = client.execute(post);// 发起请求 并返回请求的响应 if (response.getStatusLine().getStatusCode()==200) { return true; } return false; } } ~~~
';

Json与Java对象互转之Gson学习

最后更新于:2022-04-01 09:32:28

**[Json与Java对象互转之Gson学习](http://blog.csdn.net/fengyuzhengfan/article/details/39674109)** **请尊重他人的劳动成果,转载请注明出处:**[Json与Java对象互转之Gson学习 ](http://blog.csdn.net/fengyuzhengfan/article/details/39674109) 我曾在《[XML,Object,Json转换之浅析Xstream的使用](http://blog.csdn.net/fengyuzhengfan/article/details/39654025)》一文中介绍过使用XStream进行XML,Object,Json之间的转换,虽然XStream内置了对Json的支持,但是效果不是特别好,毕竟XStream重点不在Json。下面就介绍另外一种工具。 **Gson**(又称Google Gson)是Google公司发布的一个开放源代码的Java库,主要用途为串行化Java对象为JSON字符串,或反串行化JSON字符串成Java对象。 ## 1.如何使用Gson? 首先从http://code.google.com/p/google-gson/downloads/list上下载Gson的jar包,然后将其导入到项目中。然后,就可以在你的项目中使用Gson了。 ## 2.主要方法 Gson的应用主要为**toJson**与**fromJson**两个转换方法,而在使用这种对象转换之前需先创建好对象的类以及其成员才能成功的将JSON字符串成功转换成相对应的对象。 ## 3. Gson的应用实例 ~~~ /** * 使用Gson实现对象,Json互转 * @author jph * Date:2014.09.29 */ public class GsonUtil { public static Gson gson; /**Json转JavaBean**/ public static final int JSON_JAVABEAN=0x10001; /**Json转List<T>**/ public static final int JSON_LIST=0x10002; /**Json转Map<T>**/ public static final int JSON_MAP=0x10004; /** * 将对象转换成Json格式的字符串 * @param object 要转换成Json的对象 * @return String:Json格式的字符串 */ public static String convertObject2Json(Object object) { gson=new Gson(); return gson.toJson(object); } /** * 将Json转换成Java对象 * @param inputStream 要转换成Java对象的inputStream * @param javaBean List获取Map中所包含的javaBean * @param convertFlag 转换类型标识 * @return Object:Java对象 */ public static Object convertJson2Object(InputStream inputStream,Class<?>javaBean ,int convertFlag) { gson=new Gson(); Object object=null; // String json=inputStream2String(inputStream); BufferedReader reader=intputStream2BufferedReader(inputStream); Type type=getType(javaBean,convertFlag); object=gson.fromJson(reader,type); return object; } /** * 获取要转换成的对象类型 * @param javaBean * @param convertFlag * @return */ private static Type getType(Class<?> javaBean,int convertFlag) { Type type=null; switch (convertFlag) { case JSON_LIST: if (javaBean.equals(News.class)) {//Json转List泛型 type=new TypeToken<List<News>>(){}.getType(); } break; case JSON_MAP: if (javaBean.equals(News.class)) {//Json转Map泛型 type=new TypeToken<Map<String,News>>(){}.getType(); } break; case JSON_JAVABEAN://Json转JavaBean type=javaBean; break; } return type; } /** * 将InputStream封装成BufferedReader * @param inputStream * @return */ private static BufferedReader intputStream2BufferedReader(InputStream inputStream) { return new BufferedReader(new InputStreamReader(inputStream)); } } ~~~ 代码分析: 在将Json转换成对象的时候,上面代码是从服务器端获取输入流,然后将输入流封装成BufferedReader对象,然后通过fromJson()方法将Json转换成Java对象。 Gson的fromJson()方法的第一个参数支持String、JsonElement和Reader类型的参数,可以根据需要进行选择。fromJson()方法的第二个参数支持Type和Class<?>类型的参数,当将Json转成JavaBean时可以使用Class<?>参数,也就是用对应JavaBean的JavaBean.calss作为第二个参数。当需要将Json转出成List泛型,Map泛型的时候需要采用TypeToken将第二个参数转换成Type类型(TypeToken是gson提供的数据类型转换器,可以支持各种数据集合类型转换。)。
';

XML,Object,Json转换之浅析Xstream的使用

最后更新于:2022-04-01 09:32:26

[XML,Object,Json转换之浅析Xstream的使用](http://blog.csdn.net/fengyuzhengfan/article/details/39654025) 请尊重他人的劳动成果,转载请注明出处:[XML,Object,Json转换之浅析Xstream的使用 ](http://blog.csdn.net/fengyuzhengfan/article/details/39654025) XStream的是一个简单的库,主要用于Java对象和XML之间的转换。但XStream也内置了对Json的支持。 ## 1.Xstream的特点: 这里直接引用Xstream官方的叙述: **灵活易用:**在更高的层次上提供了简单、灵活、易用的统一接口,用户无需了解项目的底层细节 **无需映射:**大多数对象都可以在无需映射的情况下进行序列化与反序列化的操作 **高速稳定:**设计时力求达到的最重要的指标是解析速度快、占用内存少,以使之能够适用于大的对象处理或是对信息吞吐量要求高的系统 **清晰易懂:**项目采用reflection机制得到无冗余信息的XML文件。所生成的XML文件较本地Java序列化产物更简洁,格式更清晰,更便于用户阅读 **无需修改:**完全序列化包括private和final类型在内的全部内部字段。支持非公有类和内部类,类可以没有缺省的构造函数 **易于集成**:通过实现特定的接口,XStream可以直接与其它任何树型结构进行序列化与反序列化操作(而不仅仅是XML格式) **灵活转换:**转换策略是可以定制的,允许用户自定义特殊类型的对象如何以XML格式存储。 **错误处理:**由于XML资料不合法而造成异常时,会提供详细地诊断信息帮助处理问题。 ## 2.初始化XStream类 说Xstream简单是因为它提供统一入口,主要类XStream用作所有项目的入口点。它将重要组件集成在一起,提供更简单易用的API操作。 我们可以使用以下的语句进行初始化操作: XStreamxstream = new XStream(); 默认情况下,XStream会采用Xpp3库,XPP3是一种运行效率非常高的XML全解析实现。如果你不想依靠Xpp3库的话,也可以使用一个标准的JAXP DOM解析器,可以采用以下语句进行初始化: //不使用XPP3库 XStreamxstream = new XStream(new DomDriver()); 此xstream实例,为线程安全的,可以供多个线程进行调用,共享使用。参考 com.thoughtworks.xstream.io.xml包,会发现系统提供了多种标识解析器供我们选择,包括,DomDriver、 JDomDriver、StaxDriver等等。 前面提到了Xstream提供了对Json的支持,是因为Xstream内置了两个Driver: 1.JsonHierarchicalStreamDriver:不依赖其他类库,只实现obj->JSON 2.JettisonMappedXmlDriver:依赖jettison类库,实现 JSON->obj or obj->JSON 两种Driver在处理相同设置的Object时会得到不同的JSON串,JsonHierarchicalStreamDriver得到的串更简洁,确如官网所说。 JsonHierarchicalStreamDriver有个小问题——默认输出带格式的JSON串,结构中带空格、换行,并且没有提供修饰方式。 ## 3.常用方法: *xStream*.toXML(object):将对象转换成XML、Json。 *xStream*.toXML(obj, outputStream):将对象转换XML、Json并封装成输出流。 *xStream*.toXML(object, writer):将对象转换XML、Json并封成写入流。 *xStream*.fromXML():将XML、Json转换成对象,此方法接受File、InputStream、Reader、String、URL类型的参数。 *xStream*.alias("news", News.**class**):为指定类名创建别名。 *xStream*.useAttributeFor(News.**class**,"id"):将id设为News的元素的属性。 *xStream*.aliasField("other", BookShelf.**class**,"remark"):修改节点名称,将BookShelf类中的remark节点名修改为other。 *xStream*.addImplicitCollection(BookShelf.**class**,"books"):去掉集体节点的父节点。 *xStream*.aliasAttribute("姓名","name"):修改属性的name,为姓名。 ## 4.实例1:将对象转换成XML    ~~~ /** * 将对象转换成Xml格式的字符串 * @param object 要转换成Xml的对象 * @return String:Xml格式的字符串 */ public static String convertObject2Xml(Object object) { xStream=new XStream(); xStream.alias("news", News.class);//修改元素名称 xStream.useAttributeFor(News.class, "id");//将id设为News的元素的属性 return xStream.toXML(object); } ~~~ ## 5.实例2:将XML象转换成对象 ~~~ /** * 将成Xml格式的字符串转换成Java对象 * @param inputStream 要转换成Java对象的inputStream * @return String:Xml格式的字符串 */ public static Object convertXml2Object(InputStream inputStream) { xStream=new XStream(); xStream.alias("news", News.class);//修改元素名称 xStream.useAttributeFor(News.class, "id");//将id设为News的元素的属性 return xStream.fromXML(inputStream);//此方法也可将xml转换成map } ~~~ ## 6.实例3:将对象转换成Json ~~~ /** * 将对象转换成Json格式的字符串 * @param object 要转换成Json的对象 * @return String:Json格式的字符串 */ public static String convertObject2Json(Object object) { xStream = new XStream(newJsonHierarchicalStreamDriver() { publicHierarchicalStreamWriter createWriter(Writer out) { //删除根节点 return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE); } }); return xStream.toXML(object); } ~~~
';

Android网络编程之传递数据给服务器(二)

最后更新于:2022-04-01 09:32:24

[Android网络编程之传递数据给服务器(二)](http://blog.csdn.net/fengyuzhengfan/article/details/39628543) 请尊重他人的劳动成果,转载请注明出处:[Android网络编程之传递数据给服务器(二)](http://blog.csdn.net/fengyuzhengfan/article/details/39628543) 我曾在《[Android网络编程之传递数据给服务器(一) ](http://blog.csdn.net/fengyuzhengfan/article/details/39621469)》 一文中介绍了如何通过GET方式传递数据给服务器,通过GET方式传递数据主要适用于数据大小不超过2KB,且对安全性要求不高的情况下。下面就介绍通过POST方式传递数据主到服务器。 ## 一、通过Post方式传递数据给服务器 通过Post方式传递数据给服务器是Android应用程序开发提交数据给服务器的一种主要的方式,适用于数据量大、数据类型复杂、数据安全性高的场合。 ### 1.创建服务器端: 服务器端项目结构: ![通过Post方式传递数据给服务器——服务器端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa38e9975.jpg) ### 第一步:创建控制器Servlet ~~~ package com.jph.sp.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ServletForPOSTMethod") public class ServletForPOSTMethod extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name= request.getParameter("name"); String pwd= request.getParameter("pwd"); System.out.println("name from POST method: " + name ); System.out.println("pwd from POST method: " + pwd ); } } ~~~ 至此服务器端项目已经完成。下面开始创建Android端项目。 ### 2.创建Android端: Android端项目结构: ![通过Post方式传递数据给服务器——Android端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3906256.jpg) ### 第一步:创建Android端项目的业务逻辑层 核心代码:SendDateToServer.java: ~~~ package com.jph.sp.service; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import android.os.Handler; /** * 通过POST方式向服务器发送数据 * @author jph * Date:2014.09.27 */ public class SendDateToServer { private static String url="http://10.219.61.117:8080/ServerForPOSTMethod/ServletForPOSTMethod"; public static final int SEND_SUCCESS=0x123; public static final int SEND_FAIL=0x124; private Handler handler; public SendDateToServer(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * 通过POST方式向服务器发送数据 * @param name 用户名 * @param pwd 密码 */ public void SendDataToServer(String name,String pwd) { // TODO Auto-generated method stub final Map<String, String>map=new HashMap<String, String>(); map.put("name", name); map.put("pwd", pwd); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { if (sendPostRequest(map,url,"utf-8")) { handler.sendEmptyMessage(SEND_SUCCESS);//通知主线程数据发送成功 }else { //将数据发送给服务器失败 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } /** * 发送POST请求 * @param map 请求参数 * @param url 请求路径 * @return * @throws Exception */ private boolean sendPostRequest(Map<String, String> param, String url,String encoding) throws Exception { // TODO Auto-generated method stub //http://10.219.61.117:8080/ServerForPOSTMethod/ServletForPOSTMethod?name=aa&pwd=124 StringBuffer sb=new StringBuffer(url); if (!url.equals("")&!param.isEmpty()) { sb.append("?"); for (Map.Entry<String, String>entry:param.entrySet()) { sb.append(entry.getKey()+"="); sb.append(URLEncoder.encode(entry.getValue(), encoding)); sb.append("&"); } sb.deleteCharAt(sb.length()-1);//删除字符串最后 一个字符“&” } byte[]data=sb.toString().getBytes(); HttpURLConnection conn=(HttpURLConnection) new URL(url).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST");//设置请求方式为POST conn.setDoOutput(true);//允许对外传输数据 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 设置窗体数据编码为名称/值对 conn.setRequestProperty("Content-Length", data.length+""); OutputStream outputStream=conn.getOutputStream();//打开服务器的输入流 outputStream.write(data);//将数据写入到服务器的输出流 outputStream.flush(); if (conn.getResponseCode()==200) { return true; } return false; } } ~~~ ### 第三步:创建Activity ~~~ package com.jph.sp.activity; import com.jph.sp.service.SendDateToServer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * 通过Post方式传递数据给服务器是Android应用程序开发 * 提交数据给服务器的一种主要的方式,适用于数据量大、 * 数据类型复杂、数据安全性高的场合。 * @author jph * Date:2014.09.27 */ public class MainActivity extends Activity { private EditText edtName,edtPwd; private Button btnSend; Handler handler=new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case SendDateToServer.SEND_SUCCESS: Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); break; case SendDateToServer.SEND_FAIL: Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtName=(EditText)findViewById(R.id.edtName); edtPwd=(EditText)findViewById(R.id.edtPwd); btnSend=(Button)findViewById(R.id.btnSend); btnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String name=edtName.getText().toString(); String pwd=edtPwd.getText().toString(); if (edtName.equals("")||edtPwd.equals("")) { Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show(); }else { new SendDateToServer(handler).SendDataToServer(name, pwd); } } }); } } ~~~ 至此Android端项目已经完成了。下面就让我们看一下APP运行效果吧: Android运行效果图: ![通过Post方式传递数据给服务器——运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa391736a.jpg) ![通过Post方式传递数据给服务器——运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa39290d1.gif)
';

Android网络编程之传递数据给服务器(一)

最后更新于:2022-04-01 09:32:21

[Android网络编程之传递数据给服务器(一)](http://blog.csdn.net/fengyuzhengfan/article/details/39621469) 请尊重他人的劳动成果,转载请注明出处:[Android网络编程之传递数据给服务器(一) ](http://blog.csdn.net/fengyuzhengfan/article/details/39621469) 因为Android程序需要和服务器进行通信,所以需要服务器端提供的支持。 ## 一、通过GET方式传递数据给服务器 通过GET方式上传数据主要适用于数据大小不超过2KB,且对安全性要求不高的情况下。 ### 1.创建服务器端: 服务器端项目结构: ![通过GET方式传递数据给服务器——服务器端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa385b05d.jpg) ### 第一步:创建控制器Servlet  ~~~ package com.jph.sgm.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletForGETMethod */ @WebServlet("/ServletForGETMethod") public class ServletForGETMethod extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletForGETMethod() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获取请求的参数(使用utf-8进行解码,然后用进行ISO8859-1编码) // String name=new String(request.getParameter("name").getBytes("ISO8859-1"),"utf-8"); String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); System.out.println("name:"+name+" pwd:"+pwd); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } } ~~~ ### 第二步:测试Servlet 发布项目并在浏览器中输入:http://localhost:8080/ServerForGETMethod/ServletForGETMethod?name=aa&pwd=124 可以再控制台看到如下图的输出: ![通过GET方式传递数据给服务器——服务器端测试结果](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa386ffa2.jpg) 至此服务器端项目已经完成。下面开始创建Android端项目。 ### 2.创建Android端: Android端项目结构: ![通过GET方式传递数据给服务器——Android端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3880629.jpg) ### 第一步:创建Android端项目的业务逻辑层 核心代码:SendDateToServer.java: ~~~ package com.jph.sdg.service; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import android.os.Handler; /** * 通过GET方式向服务器发送数据 * @author jph * Date:2014.09.27 */ public class SendDateToServer { private static String url="http://10.219.61.117:8080/ServerForGETMethod/ServletForGETMethod"; public static final int SEND_SUCCESS=0x123; public static final int SEND_FAIL=0x124; private Handler handler; public SendDateToServer(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * 通过Get方式向服务器发送数据 * @param name 用户名 * @param pwd 密码 */ public void SendDataToServer(String name,String pwd) { // TODO Auto-generated method stub final Map<String, String>map=new HashMap<String, String>(); map.put("name", name); map.put("pwd", pwd); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { if (sendGetRequest(map,url,"utf-8")) { handler.sendEmptyMessage(SEND_SUCCESS);//通知主线程数据发送成功 }else { //将数据发送给服务器失败 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } /** * 发送GET请求 * @param map 请求参数 * @param url 请求路径 * @return * @throws Exception */ private boolean sendGetRequest(Map<String, String> param, String url,String encoding) throws Exception { // TODO Auto-generated method stub //http://localhost:8080/ServerForGETMethod/ServletForGETMethod?name=aa&pwd=124 StringBuffer sb=new StringBuffer(url); if (!url.equals("")&!param.isEmpty()) { sb.append("?"); for (Map.Entry<String, String>entry:param.entrySet()) { sb.append(entry.getKey()+"="); sb.append(URLEncoder.encode(entry.getValue(), encoding)); sb.append("&"); } sb.deleteCharAt(sb.length()-1);//删除字符串最后 一个字符“&” } HttpURLConnection conn=(HttpURLConnection) new URL(sb.toString()).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET");//设置请求方式为GET if (conn.getResponseCode()==200) { return true; } return false; } } ~~~ ### 第三步:创建Activity ~~~ package com.jph.sdg.activity; import com.jph.sdg.R; import com.jph.sdg.service.SendDateToServer; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * 通过GET方式向服务器发送数据,通过GET方式上传数据主要适用于数 * 据大小不超过2KB,且对安全性要求不高的情况下。 * @author jph * Date:2014.09.27 */ public class MainActivity extends Activity { private EditText edtName,edtPwd; private Button btnSend; Handler handler=new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case SendDateToServer.SEND_SUCCESS: Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show(); break; case SendDateToServer.SEND_FAIL: Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show(); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtName=(EditText)findViewById(R.id.edtName); edtPwd=(EditText)findViewById(R.id.edtPwd); btnSend=(Button)findViewById(R.id.btnSend); btnSend.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String name=edtName.getText().toString(); String pwd=edtPwd.getText().toString(); if (edtName.equals("")||edtPwd.equals("")) { Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show(); }else { new SendDateToServer(handler).SendDataToServer(name, pwd); } } }); } } ~~~ 至此Android端项目已经完成了。下面就让我们看一下APP运行效果吧: Android运行效果图: ![通过GET方式传递数据给服务器——Android端运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3890089.gif) ## 二、关于通过GET方式传递数据给服务器时,中文乱码的解决方案 当客户端向服务器发送中文时服务器端会出现乱码现象,如下图: ![关于通过GET方式传递数据给服务器时,中文乱码的解决方案](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa38aff58.jpg)   出现乱码的原因主要是,Android客户端我们采用的是UTF-8编码,而Tomcat默认采用的是ISO8859-1编码,所以会出现中文乱码的现象。 解决方案有两种: ### 第一种解决方案: 是使用UTF-8解码请求参数得到汉字,然后再通过ISO8859-1进行编码。此时服务器端的Servlet是: ~~~ package com.jph.sgm.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletForGETMethod */ @WebServlet("/ServletForGETMethod") public class ServletForGETMethod extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletForGETMethod() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获取请求的参数(使用utf-8进行解码,然后用进行ISO8859-1编码) String name=new String(request.getParameter("name").getBytes("ISO8859-1"),"utf-8"); // String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); System.out.println("name:"+name+" pwd:"+pwd); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } } ~~~   运行结果如下图: ![UTF-8解码请求参数得到汉字,然后再通过ISO8859-1进行编码](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa38c0d4d.jpg) ### 第二种解决方案: 下面我们采用过滤器的方式来解决乱码的问题: 第一步:建立一个Filter过滤器。 EncodingFilter.java ~~~ package com.jph.sgm.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; /** * Servlet Filter implementation class EncodingFilter */ @WebFilter("/*") public class EncodingFilter implements Filter { /** * Default constructor. */ public EncodingFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest req=(HttpServletRequest) request; if ("GET".equals(req.getMethod())) { HttpServletRequestEncodingWrapper wrapper=new HttpServletRequestEncodingWrapper(req); chain.doFilter(wrapper, response); }else { req.setCharacterEncoding("utf-8"); chain.doFilter(request, response); } } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } } ~~~ 上面的过滤器因为设置了过滤路径为*/所以会过滤所有的Servlet。 在上面的过滤器中用到了一个包装器,HttpServletRequestEncodingWrapper.java ~~~ package com.jph.sgm.filter; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class HttpServletRequestEncodingWrapper extends HttpServletRequestWrapper { private HttpServletRequest request; public HttpServletRequestEncodingWrapper(HttpServletRequest request) { // TODO Auto-generated constructor stub super(request); this.request=request; } @Override public String getParameter(String name) { // TODO Auto-generated method stub String value=request.getParameter(name); if (value!=null) { try {//用utf-8进行解码,然后用ISO8859-1进行编码 return new String(value.getBytes("ISO8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return super.getParameter(name); } } ~~~ 重新运行项目可以看到服务器会将客户端发来的数据用UTF-8进行解码,用ISO8859-1进行编码。运行效果图如下: ![采用过滤器的方式来解决乱码的问题](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa38d4d61.jpg) [ Android网络编程之传递数据给服务器(二)——通过POST的方式将数据传递给服务器](http://blog.csdn.net/fengyuzhengfan/article/details/39628543)
';

Android网络编程之获取网络上的Json

最后更新于:2022-04-01 09:32:19

[Android网络编程之获取网络上的Json](http://blog.csdn.net/fengyuzhengfan/article/details/39591989) 请尊重他人的劳动成果,转载请注明出处:[Android网络编程之获取网络上的Json ](http://blog.csdn.net/fengyuzhengfan/article/details/39591989) 为要获取网络上的Json所以需要服务器端提供的支持。 ## 一、创建服务器端: 服务器端项目结构: ![获取网上的Json服务器端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3819289.jpg) 服务器端运行效果图: ![获取网上的Json服务器端运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa382a315.jpg) ### 第一步:创建业务所需的JavaBean ~~~ package com.jph.sj.model; import java.util.Date; /** * 新闻实体类 * @author jph * Date:2014.09.26 */ public class News { private Integer id; private String title; private Date publishDate; public News(Integer id, String title, Date publishDate) { this.id = id; this.title = title; this.publishDate = publishDate; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getPublishDate() { return publishDate; } public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } } ~~~ ### 第二步:创建业务逻辑接口和具体实现类 业务接口: ~~~ package com.jph.sj.service; import java.util.List; import com.jph.sj.model.News; public interface NewsService { /** * 获取最新的资讯 * @return */ public List<News> getLastNews(); } ~~~ 业务接口的实现类: ~~~ package com.jph.sj.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.jph.sj.model.News; import com.jph.sj.service.NewsService; public class NewsServiceBean implements NewsService { /** * 获取最新的视频资讯 * @return */ public List<News> getLastNews(){ List<News> newes = new ArrayList<News>(); newes.add(new News(1, "李白", new Date(System.currentTimeMillis()))); newes.add(new News(2, "杜甫", new Date(System.currentTimeMillis()+8200))); newes.add(new News(3, "贾宝玉",new Date(System.currentTimeMillis()-6000))); return newes; } } ~~~ ### 第三步:创建控制器Servlet ~~~ package com.jph.sj.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jph.sj.model.News; import com.jph.sj.service.NewsService; import com.jph.sj.service.impl.NewsServiceBean; /** * 负责响应客户端的请求:http://xxx/NewsListServlet/NewsListServlet */ public class NewsListServlet extends HttpServlet { private static final long serialVersionUID = 1L; private NewsService newsService = new NewsServiceBean(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<News> newes = newsService.getLastNews();//获取最新的视频资讯 //[{id:20,title:"xxx",timelength:90},{id:10,title:"xbx",timelength:20}] StringBuilder sbJson = new StringBuilder(); //将list集合封装成Json格式的字符串 sbJson.append('['); for(News news : newes){ sbJson.append('{'); sbJson.append("id:").append(news.getId()).append(","); sbJson.append("title:\"").append(news.getTitle()).append("\","); sbJson.append("publishDate:").append(news.getPublishDate().getTime()); sbJson.append("},"); } sbJson.deleteCharAt(sbJson.length() - 1);//删除字符串末尾的逗号 sbJson.append(']'); request.setAttribute("json", sbJson.toString()); request.getRequestDispatcher("/WEB-INF/page/jsonnewslist.jsp").forward(request, response); } } ~~~ ### 第四步:创建jsonnewslist.jsp页面 ~~~ <%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%> ${json} ~~~ 至此服务器端项目已经完成。下面开始创建Android端项目。 ## 二、创建Android端: Android端项目结构: ![获取网上的Json Android端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa383a904.jpg) ### 第一步:创建业务所需的JavaBean 提示:因为服务器端和Android端项目都是用Java语言实现的,所以一些组件可以共用,JavaBean便是其中之一。此时我们在搭建Android端项目的时候,完全可以将服务器端项目中JavaBean拿来用。 ### 第二步:创建Android端项目的业务逻辑层 核心代码:GetAndParseJson: ~~~ package com.jph.gj.service; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.jph.model.News; import android.os.Handler; import android.os.Message; /** * 获取并解析网络上的Json * @author jph * Date:2014.09.26 */ public class GetAndParseJson { private String url="http://10.219.61.117:8080/ServerForJSON/NewsListServlet"; public static final int PARSESUCCWSS=0x2001; private Handler handler; public GetAndParseJson(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * 获取网络上的XML */ public void getJsonFromInternet () { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { HttpURLConnection conn=(HttpURLConnection) new URL(url).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if (conn.getResponseCode()==200) { InputStream inputStream=conn.getInputStream(); List<News>listNews=parseJson(inputStream); if (listNews.size()>0) { Message msg=new Message(); msg.what=PARSESUCCWSS;//通知UI线程Json解析完成 msg.obj=listNews;//将解析出的数据传递给UI线程 handler.sendMessage(msg); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } /** * 解析json格式的输入流转换成List * @param inputStream * @return List */ protected List<News> parseJson(InputStream inputStream) { // TODO Auto-generated method stub List<News>listNews=new ArrayList<News>(); byte[]jsonBytes=convertIsToByteArray(inputStream); String json=new String(jsonBytes); try { JSONArray jsonArray=new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jObject=jsonArray.getJSONObject(i); int id=jObject.getInt("id"); String title=jObject.getString("title"); long time=jObject.getLong("publishDate"); News news=new News(id, title, new Date(time)); listNews.add(news); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return listNews; } /** * 将输入流转化成ByteArray * @param inputStream * @return ByteArray */ private byte[] convertIsToByteArray(InputStream inputStream) { // TODO Auto-generated method stub ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte buffer[]=new byte[1024]; int length=0; try { while ((length=inputStream.read(buffer))!=-1) { baos.write(buffer, 0, length); } inputStream.close(); baos.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return baos.toByteArray(); } } ~~~ ### 第三步:创建Activity ~~~ package com.jph.gj.activity; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.jph.gj.R; import com.jph.gj.service.GetAndParseJson; import com.jph.model.News; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.widget.ListView; import android.widget.SimpleAdapter; /** * 获取网络上的Json * @author jph * Date:2014.09.26 */ public class MainActivity extends Activity { private List<News>listNews; private ListView list; Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case GetAndParseJson.PARSESUCCWSS: listNews=(List<News>) msg.obj; initData(); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list=(ListView)findViewById(R.id.list); GetAndParseJson getAndParseJson=new GetAndParseJson(mHandler); getAndParseJson.getJsonFromInternet(); } /** * 将解析后的xml填充到ListView */ protected void initData() { // TODO Auto-generated method stub List<Map<String, Object>>items=new ArrayList<Map<String,Object>>(); for (News news:listNews) { Map<String, Object>item=new HashMap<String, Object>(); item.put("id", news.getId()); item.put("title", news.getTitle()); item.put("time", convertDate(news.getPublishDate())); items.add(item); } SimpleAdapter adapter=new SimpleAdapter(this, items, R.layout.line, new String[] {"id","title","time"}, new int[]{R.id.tvId,R.id.tvTitle,R.id.tvTime}); list.setAdapter(adapter); } private String convertDate(Date publishDate) { // TODO Auto-generated method stub SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); return sdf.format(publishDate); } } ~~~ 至此Android端项目已经完成了。下面就让我们看一下APP运行效果吧: Android运行效果图: ![获取网上的Json Android端运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa384c377.jpg)
';

Android网络编程之获取网络上的XML

最后更新于:2022-04-01 09:32:17

[Android网络编程之获取网络上的XML](http://blog.csdn.net/fengyuzhengfan/article/details/39589307) **请尊重他人的劳动成果,转载请注明出处:[Android网络编程之获取网络上的XML ](http://blog.csdn.net/fengyuzhengfan/article/details/39589307)** 为要获取网络上的XML所以需要服务器端提供的支持。 ## 一、创建服务器端: 服务器端项目结构: ![服务器端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa37bf714.jpg) 服务器端运行效果图: ![获取网上的XML服务器端运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa37d2e6a.jpg) ### 第一步:创建业务所需的JavaBean ~~~ package com.jph.server.model; import java.util.Date; /** * 新闻实体类 * @author jph * Date:2014.09.26 */ public class News { private Integer id; private String title; private Date publishDate; public News(Integer id, String title, Date publishDate) { this.id = id; this.title = title; this.publishDate = publishDate; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getPublishDate() { return publishDate; } public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } } ~~~ ### 第二步:创建业务逻辑接口和具体实现类 业务接口: ~~~ package com.jph.server.service; import java.util.List; import com.jph.server.model.News; public interface XMLService { /** * 获取最新的资讯 * @return */ public List<News> getLastNews(); } ~~~ 业务接口的实现类: ~~~ package com.jph.server.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.jph.server.model.News; import com.jph.server.service.XMLService; public class XMLServiceBean implements XMLService { /** * 获取最新的视频资讯 * @return */ public List<News> getLastNews(){ List<News> newes = new ArrayList<News>(); newes.add(new News(1, "jph", new Date(System.currentTimeMillis()+100002))); newes.add(new News(2, "admin",new Date(System.currentTimeMillis()+330002))); newes.add(new News(3, "tom", new Date(System.currentTimeMillis()+180002))); return newes; } } ~~~ ### 第三步:创建控制器Servlet ~~~ package com.jph.server.xml; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jph.server.model.News; import com.jph.server.service.XMLService; import com.jph.server.service.impl.XMLServiceBean; /** * 负责响应客户端的请求:http://xxx/ServerForXML/ServletForXMLServlet */ public class ServletForXML extends HttpServlet { private static final long serialVersionUID = 1L; private XMLService newsService = new XMLServiceBean(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<News> newes = newsService.getLastNews();//获取最新的视频资讯 request.setAttribute("newes", newes); //将客户端的请求转向news.jsp页面 request.getRequestDispatcher("/WEB-INF/page/news.jsp").forward(request, response); } } ~~~ ### 第四步:使用jstl标签生成XML文件 因此需要使用jstl标签所以需要将jstl.jar与standard.jar两个jar包导入项目中。 使用jstl标签在jsp页面生成XML: news.jsp页面: ~~~ <%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><?xml version="1.0" encoding="UTF-8"?> <newslist> <c:forEach items="${newes}" var="news"> <news id="${news.id}"> <title>${news.title}</title> <publishDate>${news.publishDate}</publishDate> </news> </c:forEach> </newslist> ~~~ 至此服务器端项目已经完成。下面开始创建Android端项目。 ## 二、创建Android端: Android端项目结构: ![Android端项目结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa37e7d48.jpg) ### 第一步:创建业务所需的JavaBean 提示:因为服务器端和Android端项目都是用Java语言实现的,所以一些组件可以共用,JavaBean便是其中之一。此时我们在搭建Android端项目的时候,完全可以将服务器端项目中JavaBean拿来用。 ### 第二步:创建Android端项目的业务逻辑层 核心代码:GetXmlAndParse.java: ~~~ package com.jph.gxfi.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.xmlpull.v1.XmlPullParser; import com.jph.gxfi.model.News; import android.os.Handler; import android.os.Message; import android.util.Xml; /** * 获取网络上的xml,并解析 * @author jph * Date:2014.09.26 */ public class GetXmlAndParse { private String url="http://10.219.61.117:8080/ServerForXML/ServletForXML"; public static final int PARSESUCCWSS=0x2001; private Handler handler; public GetXmlAndParse(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * 获取网络上的XML */ public void getXml(){ new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { HttpURLConnection conn=(HttpURLConnection)new URL(url).openConnection(); conn.setConnectTimeout(5000);//设置连接超时 conn.setRequestMethod("GET"); if (conn.getResponseCode()==200) { InputStream inputStream=conn.getInputStream(); List<News> listNews=pullXml(inputStream); if (listNews.size()>0) {//如果解析结果不为空则将解析出的数据发送给UI线程 Message msg=new Message(); msg.obj=listNews; msg.what=PARSESUCCWSS; handler.sendMessage(msg); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }//和服务器建立连接 } }).start(); } /** * 解析Xml,并将其封装成 * @param inputStream */ protected List<News> pullXml(InputStream inputStream) { List<News>listNews=new ArrayList<News>(); try { XmlPullParser pullParser=Xml.newPullParser(); News news=null; pullParser.setInput(inputStream, "utf-8"); int eventCode=pullParser.getEventType(); while (eventCode!=XmlPullParser.END_DOCUMENT) { String targetName=pullParser.getName(); switch (eventCode) { case XmlPullParser.START_TAG: if ("news".equals(targetName)) {//处理news的开始节点 news =new News(); news.setId(new Integer(pullParser.getAttributeValue(0))); }else if ("title".equals(targetName)) { news.setTitle(pullParser.nextText()); }else if ("publishDate".equals(targetName)) { news.setPublishDate(new Date(pullParser.nextText())); } break; case XmlPullParser.END_TAG: if ("news".equals(targetName)) {//处理news的结束节点 listNews.add(news); } break; } eventCode=pullParser.next();//解析下一个节点(开始节点,结束节点) } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return listNews; } } ~~~ ### 第三步:创建Activity ~~~ package com.jph.gxfi.activity; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.jph.gxfi.R; import com.jph.gxfi.model.News; import com.jph.gxfi.service.GetXmlAndParse; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.ListView; import android.widget.SimpleAdapter; import android.app.Activity; /** * 获取网络上的xml * @author jph * Date:2014.09.26 */ public class MainActivity extends Activity { private List<News>listNews; private ListView list; Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case GetXmlAndParse.PARSESUCCWSS: listNews=(List<News>) msg.obj; initData(); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list=(ListView)findViewById(R.id.list); GetXmlAndParse getXmlAndParse=new GetXmlAndParse(mHandler); getXmlAndParse.getXml(); } /** * 将解析后的xml填充到ListView */ protected void initData() { // TODO Auto-generated method stub List<Map<String, Object>>items=new ArrayList<Map<String,Object>>(); for (News news:listNews) { Map<String, Object>item=new HashMap<String, Object>(); item.put("id", news.getId()); item.put("title", news.getTitle()); item.put("time", convertDate(news.getPublishDate())); items.add(item); } SimpleAdapter adapter=new SimpleAdapter(this, items, R.layout.line, new String[] {"id","title","time"}, new int[]{R.id.tvId,R.id.tvTitle,R.id.tvTime}); list.setAdapter(adapter); } private String convertDate(Date publishDate) { // TODO Auto-generated method stub SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); return sdf.format(publishDate); } } ~~~ 至此Android端项目已经完成了。下面就让我们看一下APP运行效果吧: Android运行效果图: ![获取网上的XML Android端运行效果图](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa38080a8.jpg)
';

Android开发之解析XML并实现三级联动效果

最后更新于:2022-04-01 09:32:14

请尊重他人的劳动成果,转载请注明出处:[Android开发之解析XML并实现三级联动效果](http://blog.csdn.net/fengyuzhengfan/article/details/39560427) 本实例主要应用XmlPullParser解析XML文档中的省市区,然后将数据绑定到Spinner上实现三级联动的效果。关于XmlPullParser的详解大家可以参考《[Android开发之使用PULL解析和生成XML](http://blog.csdn.net/fengyuzhengfan/article/details/39560595)》一文。 运行效果图: ![省市区三级联动](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa377f4a5.jpg) ![省市区三级联动](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3791bba.jpg) ![省市区三级联动](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa37aa213.jpg) 程序代码: 核心代码: ~~~ <pre name="code" class="java">package com.jph.sevice; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xmlpull.v1.XmlPullParser; import android.os.Handler; import android.os.Message; import android.util.Xml; /** * 解析省、市、区xml * @author jph * Date:2014.09.25 */ public class PullProvince { public static final int PARSESUCCWSS=0x2001; private Handler handler; public PullProvince(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * 获取所有省份城市以及区 * @author jph * Date:2014.09.25 */ public void getProvinces(final InputStream inStream) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(inStream, "UTF-8"); int event; event = pullParser.getEventType(); Map<String, Map<String, List<String>>> provinces = new HashMap<String, Map<String, List<String>>>();// 省份 Map<String, List<String>> cities=null;// 城市 ArrayList<String> areaAll = null; String pName = "";// 省份名称 String cName = "";// 城市名称 String aName = "";// 地区名 String targetName = "";// 当前节点的名称 while (event != XmlPullParser.END_DOCUMENT) { targetName = pullParser.getName(); switch (event) { case XmlPullParser.START_TAG: if ("province".equals(targetName)) {// 处理省份节点 pName = pullParser.getAttributeValue(0);// 当前省份名 cities=new HashMap<String, List<String>>(); } else if ("city".equals(targetName)) {// 处理城市节点 cName = pullParser.getAttributeValue(0); areaAll = new ArrayList<String>();// 地区 } else if ("area".equals(targetName)) {// 处理地区节点 aName = pullParser.getAttributeValue(0); } break; case XmlPullParser.END_TAG: if (targetName.equals("area")) { areaAll.add(aName); } else if (targetName.equals("city")) { cities.put(cName, areaAll); } else if (targetName.equals("province")) { provinces.put(pName, cities); } break; } event = pullParser.next(); } Message message=new Message(); message.obj=provinces;//将解析出的数据放到message中传给主线程 message.what=PARSESUCCWSS; handler.sendMessage(message);//通知主线程数据解析完毕 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } } ~~~ Activity: ~~~ <pre name="code" class="java">package com.jph.px; import java.io.InputStream; import java.util.List; import java.util.Map; import com.jph.sevice.PullProvince; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; /** * 省,市,区三级联动 * @author jph * Date:2014.09.25 */ public class MainActivity extends Activity { private Spinner province,city,area; private Map<String, Map<String, List<String>>> data=null; /**当前的选择的省份**/ private String currentProvince; /**当前的选择的城市**/ private String currentCity; private PullProvince pullProvince; // /**当前的选择的区**/ // private String currentArea; private Handler mHandler=new Handler(){ @SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case PullProvince.PARSESUCCWSS://数据解析完毕,加载数据 data=(Map<String, Map<String, List<String>>>) msg.obj; initData(); break; default: break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); province=(Spinner)findViewById(R.id.province); city=(Spinner)findViewById(R.id.city); area=(Spinner)findViewById(R.id.area); pullProvince=new PullProvince(mHandler); InputStream inStream = this.getClass().getClassLoader(). getResourceAsStream("province_city.xml"); pullProvince.getProvinces(inStream); } /** * 初始化数据 */ private void initData(){ if (data!=null) { String[]arrStrings= data.keySet().toArray(new String[0]); System.out.println(arrStrings); //将省份信息填充到 province Spinner province.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,arrStrings)); currentProvince=getCurrentProvince(); bindCityAdapter(currentProvince); currentCity=getCurrentCity(); bindAreaAdapter(currentCity); setOnItemSelectedListener(); } } private void bindAreaAdapter(String currentCity) { // TODO Auto-generated method stub //根据当前显示的城市将对应的区填充到area Spinner area.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, data.get(currentProvince).get(currentCity))); } private void bindCityAdapter(String currentProvince) { // TODO Auto-generated method stub //根据当前显示的省份将对应的城市填充到city Spinner city.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,data. get(currentProvince).keySet().toArray(new String[0]))); } /** * 为Spinner设置监听器 */ private void setOnItemSelectedListener() { // TODO Auto-generated method stub province.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub currentProvince=getCurrentProvince(); bindCityAdapter(currentProvince); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); city.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub currentCity=getCurrentCity(); bindAreaAdapter(currentCity); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); area.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,area.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } /**获取当前选择的省份 * @return String:当前选择的省份 */ private String getCurrentProvince() { // TODO Auto-generated method stub return province.getSelectedItem().toString(); } /**获取当前选择的城市 * @return String:当前选择的城市 */ private String getCurrentCity() { // TODO Auto-generated method stub return city.getSelectedItem().toString(); } } ~~~ 布局文件: ~~~ <pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Spinner android:id="@+id/province" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/city" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Spinner android:id="@+id/area" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ~~~
';

Android开发之Fragment详解(一)

最后更新于:2022-04-01 09:32:12

## Android开发之Fragment学习 ## 1.简介: Fragment是Android 3.0引入的新API。 Fragment代表了 Activity的子模块,因此可以把Fragment理解成Activity片段。Fragment用于自己的生命周期,也可以接受它自己的输入事件。 Fragment必须被“嵌入” Activity中使用,因此虽然Fragment也拥有自己的生命周期,但Fragment的生命周期会受它所在的Activity的生命周期的控制。例如,当Activity暂停时,该Activity内的所有Fragment都会暂停;当Activity被销毁时,该Activity内的所有Fragment都会被销毁,只有当该Activity处于活动状态时,程序员可通过方法独立地操作Fragment。 ## 2.Fragment的几个特征: 1)Fragment总是作为Activity界面的组成部分,Fragment可调用getActivity()方法获取它所在的Activity, Activity调用FragmentManager的findFragmentByld()或findFragmentByTag()方法来获取Fragment。 2)在Activity运行过程中,可调用 FragmentManager的add()、remove()、replace()方法动态地添加、删除或替换Fragment。 3)—个Activity可以同时组合多个Fragment;反过来,一个Fragment也可被多 Activity 复用。 4)Fragment可以响应自己的输入事件,并拥有自己的生命周期,但它们的生命周期直接被其所属的Activity的生命周期控制。 Android 3.0引入Fragment的初衷是为了适应大屏幕的平板电脑,由于平板电脑的屏幕比手机屏蒂更大,因此可以容纳更多的UI组件,且这些UI组件之间存在交互关系。Fragment简化了大屏幕UI的设计,它不需要开发者管理组件包含关系的复杂变化,开发者使用Fragment对UI组件进行分组、模块化管理,可以更方便地在运行过程中动态更新Activity的用户界面。 例如:有如下新闻浏览界面,该界面需要在屏幕左边显示新闻列表,并在屏幕右边显示新闻内容,此时就可以在Activity中显示两个并排的Fragment左边的Fragment显示新闻列表,右边的Fragment显示新闻内容。由于每个Fragment拥有自己的生命周期,并可响应用户输入事件,因此可以非常方便地实现:当用户单击左边列表的指定新闻时,右Fragment显示相应的新闻内容。下图左边的“平板电脑”部分显示了这种Ul界面![fragments](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2015-12-01_565daa83b3e7e.jpg) 通过使用上面的Fragment设计机制,可以取代传统的让一个Activity显示新闻列表,一个Activity显示新文内容的设计。 由于Fragment是可复用的组件,因此如果需要在正常尺寸的手机屏幕上运行该应用,可以改为使用两个 Activity,ActivityA包含 FragmentA、ActivityB包含 FragmentB。其中 ActivityA仅包含显示文章列表FragmentA,而当用户选择一篇文章时,它会启动包含新闻内容的 ActivityB,如上图右边的“手机,部分。由此可见,Fragment可以很好地支持上图所示的两种设计模式。 ## 3.创建Fragment 与创建Activity类似,开发者实现的Fragment必须继承Fragment基类,Android提供了如下图所示的Fragment继承体系。 ![Fragment结构](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3750d09.jpg) 开发者实现的Fragment,可以根据需要继承上图所示的Fragment基类或它的任意子类。接下来,实现Fragment与实现Activity非常相似,它们都需要实现与Activity类似的回调方法,例如onCreate()、onCreateView()、onStart()、onResume()、onPause()、onStop()等。 提示: 开发Fragment与开发Activity非常相似,区别只是开发Activity需要继承Activity或其子类;但开发Fragment需要继承Fragment及其子类.与此同时,只要将原来写在Activity回调方法的代码“移到”Fragment的回调方法中即可。 通常来说,创建Fragment通常需要实现如下三个方法。 1)onCreate():系统创建Fragment对象后回调该方法,实现代码中只初始化想要在 Fragment中保持的必要组件,当fragment被暂停或者停止后可以恢复。 2)onCreateView():当Fragment绘制界面组件时会回调该方法。该方法必须返回一个View,该View也就是该Fragment所显示的View。 3)onPause():当用户离开该Fragment时将会回调该方法。 对于大部分Fragment而言,通常都会重写上面这三个方法。但是实际上开发者可以根据需要重写Fragment的任意回调方法,后面将会详细介绍Fragment的生命周期及其回调方法为了控制Fragment显示的组件,通常需要重写onCreateView()方法,该方法返回的View将作为该Fragment显示的View组件。当Fragment绘制界面组件时将会回调该方法。 例如如下方法片段: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">重写该方法,该方法返回的<span lang="EN-US">View</span>将作为<span lang="EN-US">Fragment</span>显示的组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> View onCreateView(LayoutInflater inflater</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>, ViewGroup container, Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout/</span>目录下的<span lang="EN-US">fragment_book_detail.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>View rootView = inflater.inflate(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">fragment_book_detail</span></em><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>container,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">false</span></strong><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_title</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">title</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_title</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">title</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_desc</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">desc</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_desc</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">desc</span><span lang="EN-US" style="font-family:宋体; color:black">);<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> rootView;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体; color:black">}</span></p></td></tr></tbody></table> 实例:开发发显示图书详情的Fragment 下面Fragment将会显示加载一份简单的界面布局文件,并根据传入的参数来更新界面组件该Fragment的代码如下。 ### 3.1创建Fragment: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> BookDetailFragment</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Fragment</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black"> =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"item_id"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">保存该<span lang="EN-US">Fragment</span>显示的<span lang="EN-US">Book</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>BookContent.Book</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onCreate(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">如果启动该<span lang="EN-US">Fragment</span>时包含了<span lang="EN-US">ITEM_ID</span>参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (getArguments().containsKey(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> = BookContent.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_MAP</span></em><span lang="EN-US" style="font-family:宋体; color:black">.get(getArguments()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.getInt(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">));</span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//①</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">重写该方法,该方法返回的<span lang="EN-US">View</span>将作为<span lang="EN-US">Fragment</span>显示的组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> View onCreateView(LayoutInflater<span style="background:yellow">inflater</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>, ViewGroup container, Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout/</span>目录下的<span lang="EN-US">fragment_book_detail.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>View rootView =<span style="background:silver">inflater</span>.inflate(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">fragment_book_detail</span></em><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>container,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">false</span></strong><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_title</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">title</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_title</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">title</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_desc</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">desc</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_desc</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">desc</span><span lang="EN-US" style="font-family:宋体; color:black">);<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> rootView;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US"> </span></p></td></tr></tbody></table> ### 3.2创建ListFragment: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> BookDetailFragment</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Fragment</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black"> =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"item_id"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">保存该<span lang="EN-US">Fragment</span>显示的<span lang="EN-US">Book</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>BookContent.Book</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onCreate(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">如果启动该<span lang="EN-US">Fragment</span>时包含了<span lang="EN-US">ITEM_ID</span>参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (getArguments().containsKey(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> = BookContent.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_MAP</span></em><span lang="EN-US" style="font-family:宋体; color:black">.get(getArguments()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.getInt(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">));</span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//①</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">重写该方法,该方法返回的<span lang="EN-US">View</span>将作为<span lang="EN-US">Fragment</span>显示的组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> View onCreateView(LayoutInflater inflater</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>, ViewGroup container, Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout/</span>目录下的<span lang="EN-US">fragment_book_detail.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>View rootView = inflater.inflate(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">fragment_book_detail</span></em><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>container,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">false</span></strong><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black"> !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_title</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">title</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_title</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">title</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">让<span lang="EN-US">book_desc</span>文本框显示<span lang="EN-US">book</span>对象的<span lang="EN-US">desc</span>属性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>((TextView) rootView.findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_desc</span></em><span lang="EN-US" style="font-family:宋体; color:black">))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>.setText(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">book</span><span lang="EN-US" style="font-family:宋体; color:black">.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">desc</span><span lang="EN-US" style="font-family:宋体; color:black">);<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> rootView;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US"> </span></p></td></tr></tbody></table> ## 4.Fragmemt与Activity通信 为了在activity中显示Fragment还必须将Fragment添加到activity中。 ### 4.1将Fragment添加到activity中有如下两种方式: 1)在布局文件中添加:在布局文件中使用<fragment.../>元素添加Fragment,其中<fragment.../>的android:name属性必须指定Fragment的实现类。 2)在Java代码中添加:在Java代码中通过FragmentTransaction对象的relpace()或add()方法来替换或添加Fragment。 提示:Activity的getFragmentManager()方法返回FragmentManager,通过调用FragmentManager的beginTransaction()方法获取FragmentTransaction对象。 Activity的布局文件: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;?</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">xml</span><span lang="EN-US" style="font-family:Consolas"><span style="color:#7F007F">version</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1.0"</span></em><span style="color:#7F007F">encoding</span><span style="color:black">=</span><em><span style="color:#2A00FF">"utf-8"</span></em><span style="color:teal">?&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">定义一个水平排列的</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">LinearLayout</span><span style="font-family:宋体; color:#3F5FBF">,并指定使用中等分隔条</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF"> --&gt;</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">xmlns:android</span><span style="color:black">=</span><em><span style="color:#2A00FF">"http://schemas.android.com/apk/res/android"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:orientation</span><span style="color:black">=</span><em><span style="color:#2A00FF">"horizontal"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_marginLeft</span><span style="color:black">=</span><em><span style="color:#2A00FF">"16dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:layout_marginRight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"16dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:divider</span><span style="color:black">=</span><em><span style="color:#2A00FF">"?android:attr/dividerHorizontal"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">   </span><span style="color:#7F007F">android:showDividers</span><span style="color:black">=</span><em><span style="color:#2A00FF">"middle"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">添加一个</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">Fragment --&gt;</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">fragment</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:name</span><span style="color:black">=</span><em><span style="color:#2A00FF">"com.jph.fragmentdemo.BookListFragment"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/book_list"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"0dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">添加一个</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF">FrameLayout</span><span style="font-family:宋体; color:#3F5FBF">容器</span><span lang="EN-US" style="font-family:Consolas; color:#3F5FBF"> --&gt;</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">FrameLayout</span><span lang="EN-US" style="font-family:Consolas"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/book_detail_container"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"0dp"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"match_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:Consolas"><span style="">      </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"3"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal"><span lang="EN-US" style="font-family:Consolas; color:teal">&lt;/</span><span lang="EN-US" style="font-family:Consolas; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:Consolas; color:teal">&gt;</span></p></td></tr></tbody></table> Activity代码: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SelectBookActivity</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"><span style="background:silver">Activity</span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">implements</span></strong><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>BookListFragment.Callbacks</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"><span style="background:silver">onCreate</span>(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">加载<span lang="EN-US">/res/layout</span>目录下的<span lang="EN-US">main.xml</span>布局文件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>setContentView(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">main</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">实现<span lang="EN-US">Callbacks</span>接口必须实现的方法</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onItemSelected(Integer id)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建<span lang="EN-US">Bundle</span>,准备向<span lang="EN-US">Fragment</span>传入参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>Bundle arguments =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Bundle();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>arguments.putInt(BookDetailFragment.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">ITEM_ID</span></em><span lang="EN-US" style="font-family:宋体; color:black">, id);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建<span lang="EN-US">BookDetailFragment</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>BookDetailFragment fragment =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> BookDetailFragment();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">向<span lang="EN-US">Fragment</span>传入参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>fragment.setArguments(arguments);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">使用<span lang="EN-US">fragment</span>替换<span lang="EN-US">book_detail_container</span>容器当前显示的<span lang="EN-US">Fragment</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>getFragmentManager().beginTransaction()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>.replace(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">book_detail_container</span></em><span lang="EN-US" style="font-family:宋体; color:black">, fragment).commit();<span style=""> </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//①</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体; color:black">}</span></p></td></tr></tbody></table> 上而的程序中①号粗体字代码就调用了FragmentTransaction的replace()方法动态更新了 ID为book_detail_container容器(也就是前面布局文件中的FrameLayout容器)中显示的 Fragment。 将Fragment添加到Activity之后,Fragment必须与Activity交互信息,这就需要Fragment能获取它所在的Activity, Activity也能获取它所包含的任意的Fragment。可按如下方法进行。 ### 4.2在activity中获取Fragement,在Fragment中获取activity的方法: 1)Fragment获取它所在的Activity:调用Fragment的getActivity()方法即可返回它所在的Activity。 2)Activity获取它包含的Fragment:调用 Activity关联的 FragmentManager的 findFragmentByld(int id)或findFragmentByTag(String tag)方法即可获取指定的 Fragment。 提示: 在界面布局文件中使用<fragment.../>元素添加Fragment时,可以为<fragment.../>元素指定android:id或android:tag属性,这两个属性都可用于标识该 Fragment,接下来 Activity将可通过 findFragmentByld(int id)或 findFragmentByTag(String tag)来获取该 Fragment。 ### 4.3Fragment与Activity可能还需要相互传递数据的方式: 1)Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将 Bundle数据包传给 Fragment。 2)Fragment向Activity传递数据或Activity需要在Fragment运行中进行实时通信:在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口,这样Fragment即可调用该回调方法将数据传给Activity。 3)通过广播的方式。 ## 5.Fragment 管理与 Fragment事务 前面介绍了 Activity与Fragment交互相关的内容,其实Activity管理Fragment主要依靠FragmentManger。 ### 5.1FragmentMange的功能: 1)使用 findFragmentByld()或 findFragmentByTag()方法来获取指定 Fragment。 2)调用popBackStack()方法将Fragment从后台找中弹出(模拟用户按下BACK按键)。 3)调用addOnBackStackChangeListener()注册个监听器,用于监听后台栈的变化。如果需要添加、删除、替换Fragment,则需要借助FragmentTransaction对象, FragmentTransaction 代表 Activity对 Fragment执行的多个改变。 提示: FragmentTransaction也被翻译为Fragment事务。与数据库事务类似的是,数据库事务代表了对底层数组的多个更新操作;而Fragment事务则代表了Activity对Fragment执行的多个改变操作。 每个FragmentTransaction可以包含多个对Fragment的修改,比如包含调用多个add()、replace()、和remove()操作,最后调用commit()提交事务即可。 在调用commit()之前,开发者也可调用addToBackStack()将事务添加到back栈,该栈由Activity负责管理,这样允许用户按BACK按键返回到前一个Fragment状态。 <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" width="568" style="width:426.1pt; margin-left:10.5pt; border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建一个新的<span lang="EN-US">Fragment</span>并打开事务</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">Fragment newFragment =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ExampleFragment();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">FragmentTransaction transaction = getFragmentManager().beginTransaction();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">替换该界面中<span lang="EN-US">fragment_container</span>容器内的<span lang="EN-US">Fragment</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">transaction.replace(R.id.fragment_container, newFragment);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">将事务添加到<span lang="EN-US">back</span>栈,允许用户按<span lang="EN-US">back</span>按键返回到替换<span lang="EN-US">Fragment</span>之前的状态</span><span lang="EN-US" style="font-family:宋体; color:black">transaction.addToBackStack(null);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">提交事务</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体; color:black">transaction.commit();</span></p></td></tr></tbody></table> 在上面的示例代码中,newFragment替换了当前界面布局中ID为fragment_container的容器内的Fragment,由于程序调用了addToBackStack()将该replace操作添加到了back栈中,因此用户可以通过按下BACK按键返回替换之前的状态。 ## 6.Fragment的生命周期: 与Activity类似的是,Fragment也存在如下状态。 Ø 活动状态:当前Fragment位于前台,用户可见,可以获得焦点。 Ø 暂停状态:其他Activity位于前台,该Fragment依然可见,只是不能获得焦点。 Ø 停止状态:该Fragment不可见,失去焦点。 Ø 销毁状态:该Fragment被完全删除,或该Fragment所在的Activity被结束。 ![fragment的生命周期](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa37611e9.jpg) 从上图可以看出,在Fragment的生命周期中,如下方法会被系统回调。 Ø onAttach():当该Fragment被添加到Activity时被回调。该方法只会被调用一次。 Ø onCreate(Bundle savedStatus):创建 Fragment时被回调。该方法只会被调用一次。 Ø onCreateView():每次创建、绘制该Fragment的 View 组件时回调该方法,Fragment将会显示该方法返回的 View组件。 Ø onActivityCreated():当 Fragment所在的 Activity被启动完成后回调该方法。 Ø onStart():启动 Fragment时被回调。 Ø onResume():恢复 Fragment时被回调,onStart()方法后一定会回调()onResume()方法。 Ø onPause():暂停Fragment时被回调。 Ø onStop():停止Fragment时被回调。 Ø onDestroyView():销毁该 Fragment所包含的 View 组件时调用。 Ø onDestroy():销毁Fragment时被回调。该方法只会被调用一次。 Ø onDetach():将该 Fragment从 Activity中被删除、被替换完成时回调该方法,onDestroy()方法后一定会回调onDetach()方法。该方法只会被调用一次。
';

Android开发之获取所有软件信息

最后更新于:2022-04-01 09:32:10

程序运行效果图: ![获取软件信息](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa36d7d5c.jpg) ![扫描并启动手机上的软件](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-02-19_56c6aa3709ad2.gif) 程序代码: ~~~ /** * 获取所有软件信息 * 1.通过异步的方式显示系统中所有软件 * 2.单击打开指定软件 * 3.将所有软件的包名和activity名保存的本地SharedPreferences * @author jph * Date:2014.09.21 */ public class ScanPackage1 extends Activity { /**扫描成功**/ private final static int FLAG_LOAD_SUCCESS=0x10001; private final static int SCANNING=0x10002; private ListView list; private List<Map<String, Object>>items=new ArrayList<Map<String,Object>>(); private SimpleAdapter adapter; // 取得所有安装软件信息 private List<PackageInfo> allPackageInfos; // 取得自己安装的软件信息 private List<PackageInfo> userPackageInfos; // 取得系统安装的软件信息 private List<PackageInfo> sysPackageInfos; Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case FLAG_LOAD_SUCCESS://完成扫描 break; case SCANNING://正在扫描 items.add((Map<String, Object>) msg.obj); //通知适配器数据改变 adapter.notifyDataSetChanged(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sp_layout); list=(ListView)findViewById(R.id.list); new ScanThread().start(); adapter=new SimpleAdapter(this, items, R.layout.line, new String[]{ "imgIco","appName","packageName" }, new int[]{R.id.imgIco,R.id.tvAppName,R.id.tvAppDesc}); list.setAdapter(adapter); //ViewBinder该类可以帮助SimpleAdapter加载图片(如:Bitmap,Drawable) adapter.setViewBinder(new ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { // TODO Auto-generated method stub if(view instanceof ImageView && data instanceof Drawable){ ImageView iv = (ImageView) view; iv.setImageDrawable((Drawable) data); return true; }else{ return false; } } }); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub try { PackageInfo pInfo=allPackageInfos.get(arg2); Intent intent=new Intent(); intent.setComponent(new ComponentName(pInfo.packageName, pInfo.activities[0].name)); startActivity(intent); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }); } // *********--------*创建一个线程加载安装程序*--------------***********// private class ScanThread extends Thread { @Override public void run() { // 取得系统安装所有软件信息 allPackageInfos = getPackageManager().getInstalledPackages( PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); // 定义用户安装软件信息包 userPackageInfos = new ArrayList<PackageInfo>(); // 定义系统安装软件信息包 sysPackageInfos = new ArrayList<PackageInfo>(); // 循环取出所有软件信息 for (int i = 0; i < allPackageInfos.size(); i++) { // 得到每个软件信息 PackageInfo temp = allPackageInfos.get(i); ApplicationInfo appInfo = temp.applicationInfo; if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 || (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { //系统软件 sysPackageInfos.add(temp); } else { //用户自己安装软件 userPackageInfos.add(temp); } //获取程序的图标 Drawable ico=ScanPackage1.this.getPackageManager().getApplicationIcon(appInfo); //获取程序的名称 String appName=(String) ScanPackage1.this.getPackageManager().getApplicationLabel(appInfo); Map<String, Object>item=new HashMap<String, Object>(); //获取程序的包名 String packageName=appInfo.packageName; item.put("imgIco", ico); item.put("appName", appName); item.put("packageName", packageName); Message message = new Message(); message.what =SCANNING; message.obj = item; mHandler.sendMessage(message); } saveInfo(sysPackageInfos, userPackageInfos); mHandler.sendEmptyMessage(FLAG_LOAD_SUCCESS); } }; /** * 将系统中所装程序的信息写入到配置文件 * @param sysPackageInfos 系统安装软件信息包 * @param userPackageInfos 用户安装软件信息包 */ private void saveInfo(List<PackageInfo> sysPackageInfos,List<PackageInfo> userPackageInfos) { //将用户安装的软件添加到添加到系统软件的集合中 sysPackageInfos.addAll(userPackageInfos); SharedPreferences sp = this.getSharedPreferences("appInfs", MODE_PRIVATE); Editor editor = sp.edit(); for (int i = 0; i < sysPackageInfos.size(); i++) { try { //获取程序的包名 String packageName = sysPackageInfos.get(i).packageName; // 取出activity信息 ActivityInfo activityInfo = sysPackageInfos.get(i).activities[0]; // 取出activity名字 String activityName=activityInfo.name; //将程序的信息写入到配置文件 editor.putString(packageName, activityName); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } editor.commit(); } } ~~~
';

Android开发之使用Web Service进行网络编程

最后更新于:2022-04-01 09:32:07

## 使用Web Service进行网络编程 Android应用通常都是运行在手机平台上,手机系统的硬件资源是有限的,不管是存储能力还是计算能力都有限,在Android系统上开发、运行一些单用户、小型应用是可能的, 但对于需要进行大量的数据处理、复杂计算的应用,还是只能部署在远程服务器上,Android应用将只是充当这些应用的客户端。 为了让Android应用与远程服务器之间进行交互,可以借助子Java的RMI技术,但这要求远程服务器程序必须采用Java实现;也可以借助于CORBA技术,但这种技术显得过于复杂,除此之外,Web Service是一种不错的选择。 ## 1.Web Service 平台概述 Web Service平台主要涉及的技术有SOAP(Simple Object Access Protocol,简单对象访问协议),WSDL( Web Service Description Language,Web Service描述语言),UDDI(UniversalDescription, Description and Integration,统一描述、发现和整合协议)。 ### 1.1. SOAP (简单对象访问协议) SOAP (SimpleObject Access Protocol,简单对象访问协议)是一种具有扩展性的;XML消息协议。SOAP允许一个应用程序向另一个应用程序发送XML消息,SOAP消息是从SOAP发送者传至SOAP接收者的单路消息,任何应用程序均可作为发送者或接收者。SOAP仅定义消息结构和消息处理的协议,与底层的传输协议独立。因此,SOAP协议能通过HTTP, JMS 或SMTP协议传输。 SOAP依赖于XML文档来构建,一条SOAP消息就是一份特定的XML文档,SOAP消息包合如下三个主要元素: Ø必需的<Envelope.../>根元素,SOAP消息对应的XML文档以该元素作为根元素。 Ø可选的<Header../>元素,包含SOAP消息的头信息。 Ø必需的<Body../>元素,包含所有的调用和响应信息。 就目前的SOAP消息的结构来看,<Envelope.../>根元素的通常只能包含两个子元素,第一个子元素是可选的<Header../>元素,第二个子元素是必需的<Body../>元素。 ### 1.2. WSDL(WebService描述语言) WSDL (WebService Description Language, Web Service描述语言)使用 XML描述Web Service,包括访问和使用WebService所必需的信息,定义该Web Service的位置、功能及如何通信等描述信息。 一般来说,只要调用者能够获取WebService对应的WSDL,就可以从中了解它所提供的服务及如何调用Web Service。因为一份WSDL文件淸晰地定义了三个方面的内容。 ØWHAT部分:用于定义Web Service所提供的操作(或方法),也就是Web Service能做些什么。由WSDL中的<types. ../>、<message…/>、和<portTyp…/>元素定义。 ØHOW部分:用于定义如何访问Web Service,包括数据格式详情和访问Web Service操作的必要协议。也就是定义了如何访问Web Service。 ØWHERE部分:用于定义Web Service位于何处,如何使用特定协议决定的网络地址(如URL)指定。该部分使用<service.../>元素定义,可在WSDL文件的最后部分看到<service.../>元素。 一份WSDL文档通常可分为两个部分: Ø第一个部分定义了服务接口,它在WSDL中由<message.../>元素和<portType…/>两个元素组成,其中<message.../>元素定义了操作的交互方式。而<portType…/>元素里则可包含任意数量的<operation.../>元素,每个<operation.../>元素代表一个允许远程调用的操作(即方法)。 ØWSDL的第二个部分定义了服务实现,它在WSDL中由<binding.../>元素和 <service.../>两个元素组成,其中<binding.../>定义使用特定的通信协议、数据编码模型和底层通信协议,将Web Service服务接口定义映射到具体实现。而 <service.../>元素则包含一系列的<portType…/>子元素,< portType.../>子元素将会把绑定机制、服务访问协议和端点地址结合在一起。 ### 1.3. UDDI(统一描述、发现和整合协议) UDDI (UniversalDescription, Description and Integration,统一描述、发现和整合协议)是一套信息注册规范,它具有如下特点: Ø基于Web。 Ø分布式。 UDDI包括一组允许企业向外注册WebService、以使其他企业发现访问的实现标准。 UDDI的核心组件是UDDI注册中心,它使用XML文件来描述企业及其提供的Web Service, 通过使用UDDI, Web Service提供者可以对外注册Web Service,从而允许其他企业来调用该企业注册的Web Service。Web Service提供者通过UDDI注册中心的Web界面,将它所供的Web Service的信息加入UDDI注册中心,该Web Service就可以被发现和调用。 Web Service使用者也通过UDDI注册中心査找、发现自己所需的服务。当Web Service使用者找到自己所需的服务之后,可以将自己绑定到指定的Web Service提供者,再根据该 Web Service对应的WSDL文档来调用对方的服务。 ## 2. 使用Android启用调用Web Service Java本身提供了丰富的WebService支持,比如Sun公司制定的JAX-WS 2规范,还有 Apache开源组织所提供的Axis1、Axis2、CXF等,这些技术不仅可以用于非常方便地对外提供Web Service,也可以用于简化Web Service的客户端编程。 对于手机等小型设备而言,它们的计算资源、存储资源都十分有限,因此Android应用不大可能需要对外提供Web Service,Android应用通常只是充当Web Service的客户端,调用远程Web Serice。 Google为Android平台开发WebService客户端提供了 ksoap2-android项目,但这个项目并未直接集成在Android平台中,还需要开发人员自行下载。 ### 2.1为Android应用增加ksoap2-android支持的下步骤。 1)登录http://code.google.eom/p/ksoap2-android/站点,该站站点有介绍下载ksoap2-androi项目的方法。 2)下载 ksoap2-android项目的ksoap2-android-assembly-3.0.0-RC4.jar-with-dependencies. jar包。 3)将下载的jar包放到android项目的libs目录下即可。 为Android项目添加了ksoap2-android包之后,接下来借助   ksoap2-android项目来调用WebService所暴露出来的操作。 ### 2.2使用ksoap2-android调用Web Service操作的步驟如下: 1)创建HttpTransportSE对象,该对象用于调用WebService操作。 2)创建 SoapSerializationEnvelope对象。 提示:从名称来看SoapSerializationEnvelope代表一个SOAP消息封包;但ksoap2-android项目对 SoapSerializationEnvelope的处理比较特殊,它是HttpTransportSE调用WebService时信息的载体;客户端需要传入的参数,需要通过SoapSerializationEnvelope对象的bodyOut属性传给服务器;服务器响应生成的SOAP消息也通过该对象的body  Out属性来获取。 3)创建SoapObject对象,创建该对象时需要传入所要调用WebService的命名空间、Web Service方法名。 4)如果有参数需要传给Web Service服务器端,调用SoapObject对象的addProperty(Stringname,Object value)方法来设置参数,该方法的name参数指定参数名;value参数指定参数值。 5)调用SoapSerializationEnvelope的setOutputSoapObject()方法,或者直接对bodyOut属性赋值,将前两步创逆的SoapObject对象设为SoapSerializationEnvelope的传出SOAP消息体。 6)调用对象的call()方法,并以SoapSerializationEnvelope作为参数调用远程WebService。 7)调用完成后,访问SoapSerializationEnvelope对象的bodyln属性,该属性返回一个SoapObject对象,该对象就代表了Web Service的返回消息。解析该SoapObject对象,即可获取调用Web Service的返回值。 ### 2.3实例:调用Web Service实现天气预报 在开发天气预报的Android应用之前,首先需要找到一个可以对外提供天气预报的Web Service,通过搜索,发现http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx站点可以对外提供天气预报的WebService,因此程序将会调用该站点的Web Service来实现天气预报。 为了让应用界面更加美观,可以访问http://www.webxml.com.cn/images/weather.zip下载各种天气图标,可以使用这些天气图标来美化应用。 本程序主要需要调用如下三个Web Seivice操作: 获取省份。 根据省份获取城市。 根据城市获取天气。 为了调用上面的三个WebService应用程序提供如下工具类。 <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid green 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> WebServiceUtil</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">定义<span lang="EN-US">Web Service</span>的命名空间</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black"> =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"http://WebXml.com.cn/"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">定义<span lang="EN-US">Web Service</span>提供服务的<span lang="EN-US">URL</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_URL</span></em><span lang="EN-US" style="font-family:宋体; color:black"> =</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">调用远程<span lang="EN-US">Web Service</span>获取省份列表</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> List&lt;String&gt; getProvinceList()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">调用的方法</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String methodName =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"getRegionProvince"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建<span lang="EN-US">HttpTransportSE</span>传输对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> HttpTransportSE ht =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> HttpTransportSE(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_URL</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>ht.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">debug</span><span lang="EN-US" style="font-family:宋体; color:black"> =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">true</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">使用<span lang="EN-US">SOAP1.1</span>协议创建<span lang="EN-US">Envelop</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapSerializationEnvelope envelope =</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapSerializationEnvelope(SoapEnvelope.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">VER11</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">实例化<span lang="EN-US">SoapObject</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>SoapObject soapObject =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapObject(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black">, methodName);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">bodyOut</span><span lang="EN-US" style="font-family:宋体; color:black"> = soapObject;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">设置与<span lang="EN-US">.Net</span>提供的<span lang="EN-US">Web Service</span>保持较好的兼容性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">dotNet</span><span lang="EN-US" style="font-family:宋体; color:black"> =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">true</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;<span style="">   </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>FutureTask&lt;List&lt;String&gt;&gt; task =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> FutureTask&lt;List&lt;String&gt;&gt;(</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Callable&lt;List&lt;String&gt;&gt;()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> List&lt;String&gt; call()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">throws</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Exception</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">调用<span lang="EN-US">Web Service</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>ht.call(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black"> + methodName, envelope);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (envelope.getResponse() !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">获取服务器响应返回的<span lang="EN-US">SOAP</span>消息</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>SoapObject result = (SoapObject) envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">bodyIn</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>SoapObject detail = (SoapObject) result.getProperty(</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                       </span>methodName +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"Result"</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">解析服务器响应的<span lang="EN-US">SOAP</span>消息。</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"><em>parseProvinceOrCity</em>(detail);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>});</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Thread(task).start();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">try</span></strong><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> task.get();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">catch</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (Exception e)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>e.printStackTrace();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">根据省份获取城市列表</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> List&lt;String&gt; getCityListByProvince(String province)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">调用的方法</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String methodName =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"getSupportCityString"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">创建<span lang="EN-US">HttpTransportSE</span>传输对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> HttpTransportSE ht =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> HttpTransportSE(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_URL</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>ht.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">debug</span><span lang="EN-US" style="font-family:宋体; color:black"> =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">true</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">实例化<span lang="EN-US">SoapObject</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>SoapObject soapObject =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapObject(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black">, methodName);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">添加一个请求参数</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>soapObject.addProperty(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"theRegionCode"</span><span lang="EN-US" style="font-family:宋体; color:black">, province);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">使用<span lang="EN-US">SOAP1.1</span>协议创建<span lang="EN-US">Envelop</span>对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapSerializationEnvelope envelope =</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapSerializationEnvelope(SoapEnvelope.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">VER11</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">bodyOut</span><span lang="EN-US" style="font-family:宋体; color:black"> = soapObject;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">设置与<span lang="EN-US">.Net</span>提供的<span lang="EN-US">Web Service</span>保持较好的兼容性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">dotNet</span><span lang="EN-US" style="font-family:宋体; color:black"> =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">true</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>FutureTask&lt;List&lt;String&gt;&gt; task =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> FutureTask&lt;List&lt;String&gt;&gt;(</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Callable&lt;List&lt;String&gt;&gt;()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> List&lt;String&gt; call()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">throws</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Exception</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">调用<span lang="EN-US">Web Service</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>ht.call(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black"> + methodName, envelope);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (envelope.getResponse() !=</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">获取服务器响应返回的<span lang="EN-US">SOAP</span>消息</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>SoapObject result = (SoapObject) envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">bodyIn</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>SoapObject detail = (SoapObject) result.getProperty(</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                       </span>methodName +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"Result"</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">解析服务器响应的<span lang="EN-US">SOAP</span>消息。</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"><em>parseProvinceOrCity</em>(detail);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>});</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Thread(task).start();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">try</span></strong><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> task.get();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">catch</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (Exception e)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>e.printStackTrace();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> List&lt;String&gt; parseProvinceOrCity(SoapObject detail)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>ArrayList&lt;String&gt; result =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ArrayList&lt;String&gt;();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">for</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> i = 0; i &lt; detail.getPropertyCount(); i++)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">解析出每个省份</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>result.add(detail.getProperty(i).toString().split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">","</span><span lang="EN-US" style="font-family:宋体; color:black">)[0]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> result;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">static</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapObject getWeatherByCity(String cityName)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> String methodName =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"getWeather"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> HttpTransportSE ht =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> HttpTransportSE(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_URL</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>ht.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">debug</span><span lang="EN-US" style="font-family:宋体; color:black"> =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">true</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">nbsp;      </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">final</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapSerializationEnvelope envelope =</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapSerializationEnvelope(SoapEnvelope.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">VER11</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>SoapObject soapObject =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapObject(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black">, methodName);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>soapObject.addProperty(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"theCityCode"</span><span lang="EN-US" style="font-family:宋体; color:black">, cityName);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">bodyOut</span><span lang="EN-US" style="font-family:宋体; color:black"> = soapObject;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">设置与<span lang="EN-US">.Net</span>提供的<span lang="EN-US">Web Service</span>保持较好的兼容性</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">dotNet</span><span lang="EN-US" style="font-family:宋体; color:black"> =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">true</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>FutureTask&lt;SoapObject&gt; task =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> FutureTask&lt;SoapObject&gt;(</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Callable&lt;SoapObject&gt;()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> SoapObject call()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">throws</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Exception</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>ht.call(</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">SERVICE_NS</span></em><span lang="EN-US" style="font-family:宋体; color:black"> + methodName, envelope);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>SoapObject result = (SoapObject) envelope.</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">bodyIn</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>SoapObject detail = (SoapObject) result.getProperty(</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>methodName +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"Result"</span><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> detail;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>});</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Thread(task).start();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">try</span></strong><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> task.get();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">catch</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (Exception e)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>e.printStackTrace();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US"> </span></p></td></tr></tbody></table> 上面的程序调用Web Service的方法还是没有改变,前面两个方法——获取系统支持的省份列表,根据省份获取城市列表——将远程Web Service返回的数据解析成List<String>后返回,这样方便Android应用使用。由于第二个方法需要返回的数据量较多,所以程序直接返回了 SoapObject 对象。 上面的程序中调用WebService时将SoapSerializationEnvelope对象的dotNet属性设为 true——因为上面这个网站是通过.NET来对外提供WebService的,因此需要将 SoapSerializationEnvelope对象的 dotNet 属性设为 true。 有了上面的调用WebService的工具类之后,接下来可以在Activity中使用该工具类来获取天气服务信息。该Activity使用了两个Spinner让用户选择省份、城市,当用户选择指定城市后,系统将会加载该程序的天气信息。 该程序的界面布局代码如下: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span class="GramE"><span lang="EN-US" style="font-family:宋体; color:teal">&lt;?</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">xml</span></span><span lang="EN-US" style="font-family:宋体"><span style="color:#7F007F">version</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1.0"</span></em><span style="color:#7F007F">encoding</span><span style="color:black">=</span><em><span style="color:#2A00FF">"utf-8"</span></em><span style="color:teal">?&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体"><span style="color:#7F007F">xmlns:android</span><span style="color:black">=</span><em><span style="color:#2A00FF">"http://schemas.android.com/apk/res/android"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">   </span><span style="color:#7F007F">android:orientation</span><span style="color:black">=</span><em><span style="color:#2A00FF">"vertical"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">   </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">   </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:orientation</span><span style="color:black">=</span><em><span style="color:#2A00FF">"horizontal"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">TextView</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:hint</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@string/province"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">让用户选择省份的<span lang="EN-US">Spinner --&gt;</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">Spinner</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/province"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span><span style="color:black"><span style=""> </span></span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">TextView</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:hint</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@string/city"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">让用户选择城市的<span lang="EN-US">Spinner --&gt;</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">Spinner</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/city"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span><span style="color:black"><span style=""> </span></span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;/</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体; color:teal">&gt;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">TextView</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">   </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/weatherCurrent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">   </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">   </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">显示今天天气的图片和文本框<span lang="EN-US"> --&gt;</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><u><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">ImageView</span></u><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/todayWhIcon1"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><u><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">ImageView</span></u><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/todayWhIcon2"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">TextView</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/weatherToday"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><u><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></u></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;/</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体; color:teal">&gt;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">显示明天天气的图片和文本框<span lang="EN-US"> --&gt;</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><u><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">ImageView</span></u><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/tomorrowWhIcon1"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><u><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">ImageView</span></u><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/tomorrowWhIcon2"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">TextView</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/weatherTomorrow"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><u><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></u></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;/</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体; color:teal">&gt;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F5FBF">&lt;!--</span><span style="font-family:宋体; color:#3F5FBF">显示后天天气的图片和文本框<span lang="EN-US"> --&gt;</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">       </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><u><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">ImageView</span></u><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/afterdayWhIcon1"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><u><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">ImageView</span></u><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/afterdayWhIcon2"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">TextView</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:id</span><span style="color:black">=</span><em><span style="color:#2A00FF">"@+id/weatherAfterday"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><u><span style="color:#7F007F">android:layout_width</span><span style="color:black">=</span><em><span style="color:#2A00FF">"fill_parent"</span></em></u></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_height</span><span style="color:black">=</span><em><span style="color:#2A00FF">"wrap_content"</span></em></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"><span style="">           </span><span style="color:#7F007F">android:layout_weight</span><span style="color:black">=</span><em><span style="color:#2A00FF">"1"</span></em><span style="color:teal">/&gt;</span></span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:teal">&lt;/</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体; color:teal">&gt;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:teal">&lt;/</span><span lang="EN-US" style="font-family:宋体; color:#3F7F7F">LinearLayout</span><span lang="EN-US" style="font-family:宋体; color:teal">&gt;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体"> </span></p></td></tr></tbody></table> 当程序加载时,程序会调用WebServiceUtil的getProvinceList()方法来获取省份列表,并 使用第一个Spinner加载、显示所有省份:当用户改变选择了省份之后,程序会调用 WebServiceUtil的getCityListByProvince(Stringprovince)方法来获取该省份的全部城市:当用户改变选择城市之后,程序会调用WebServiceUtil的getWeatherByCity(StringcityName)方法 获取该城市的天气。 该Activity的代码如下: <table class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse; border:none"><tbody><tr style=""><td width="568" valign="top" style="width:426.1pt; border:solid windowtext 1.0pt; padding:0cm 5.4pt 0cm 5.4pt"><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">class</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> GetWeather</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">extends</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Activity</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black">{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Spinner</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">provinceSpinner</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> Spinner</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">citySpinner</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ImageView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ImageView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> TextView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherToday</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ImageView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ImageView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> TextView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherTomorrow</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ImageView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ImageView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> TextView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherAfterday</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> TextView</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherCurrent</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onCreate(Bundle savedInstanceState)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">super</span></strong><span lang="EN-US" style="font-family:宋体; color:black">.onCreate(savedInstanceState);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>setContentView(R.layout.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">main</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black"> = (ImageView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon1</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black"> = (ImageView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon2</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherToday</span><span lang="EN-US" style="font-family:宋体; color:black"> = (TextView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">weatherToday</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black"> = (ImageView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon1</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black"> = (ImageView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon2</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherTomorrow</span><span lang="EN-US" style="font-family:宋体; color:black"> = (TextView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">weatherTomorrow</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black"> = (ImageView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon1</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black"> = (ImageView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon2</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherAfterday</span><span lang="EN-US" style="font-family:宋体; color:black"> = (TextView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">weatherAfterday</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherCurrent</span><span lang="EN-US" style="font-family:宋体; color:black"> = (TextView) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">weatherCurrent</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">获取程序界面中选择省份、城市的<span lang="EN-US">Spinner</span>组件</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">provinceSpinner</span><span lang="EN-US" style="font-family:宋体; color:black"> = (Spinner) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">province</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">citySpinner</span><span lang="EN-US" style="font-family:宋体; color:black"> = (Spinner) findViewById(R.id.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">city</span></em><span lang="EN-US" style="font-family:宋体; color:black">);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">调用远程<span lang="EN-US">Web Service</span>获取省份列表</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>List&lt;String&gt; provinces = WebServiceUtil.<em>getProvinceList</em>();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>ListAdapter adapter =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ListAdapter(</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">this</span></strong><span lang="EN-US" style="font-family:宋体; color:black">, provinces);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">使用<span lang="EN-US">Spinner</span>显示省份列表</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">provinceSpinner</span><span lang="EN-US" style="font-family:宋体; color:black">.setAdapter(adapter);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">当省份<span lang="EN-US">Spinner</span>的选择项被改变时</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">provinceSpinner</span><span lang="EN-US" style="font-family:宋体; color:black">.setOnItemSelectedListener(</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> OnItemSelectedListener()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onItemSelected(AdapterView&lt;?&gt; source, View parent,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> position,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">long</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> id)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>List&lt;String&gt; cities = WebServiceUtil</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>.<em>getCityListByProvince</em>(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">provinceSpinner</span><span lang="EN-US" style="font-family:宋体; color:black">.getSelectedItem()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                       </span>.toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>ListAdapter cityAdapter =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> ListAdapter(GetWeather.</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">this</span></strong><span lang="EN-US" style="font-family:宋体; color:black">,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">                   </span>cities);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">使用<span lang="EN-US">Spinner</span>显示城市列表</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">citySpinner</span><span lang="EN-US" style="font-family:宋体; color:black">.setAdapter(cityAdapter);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onNothingSelected(AdapterView&lt;?&gt; arg0)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>});</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">当城市<span lang="EN-US">Spinner</span>的选择项被改变时</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">citySpinner</span><span lang="EN-US" style="font-family:宋体; color:black">.setOnItemSelectedListener(</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> OnItemSelectedListener()</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onItemSelected(AdapterView&lt;?&gt; source, View parent,</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> position,</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">long</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> id)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">               </span>showWeather(</span><span lang="EN-US" style="font-family:宋体; color:#0000C0">citySpinner</span><span lang="EN-US" style="font-family:宋体; color:black">.getSelectedItem().toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><span lang="EN-US" style="font-family:宋体; color:#646464">@Override</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">public</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> onNothingSelected(AdapterView&lt;?&gt; arg0)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>});</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">void</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> showWeather(String city)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>String weatherToday =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>String weatherTomorrow =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>String weatherAfterday =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>String weatherCurrent =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> iconToday[] =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black">[2];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> iconTomorrow[] =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black">[2];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> iconAfterday[] =</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">new</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black">[2];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">获取远程<span lang="EN-US">Web Service</span>返回的对象</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>SoapObject detail = WebServiceUtil.<em>getWeatherByCity</em>(city);<span style="">     </span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">获取天气实况</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherCurrent = detail.getProperty(4).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">解析今天的天气情况</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>String date = detail.getProperty(7).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherToday =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"</span><span style="font-family:宋体; color:#2A00FF">今天:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体; color:black"> + date.split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">" "</span><span lang="EN-US" style="font-family:宋体; color:black">)[0];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherToday = weatherToday +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">天气:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体; color:black"> + date.split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">" "</span><span lang="EN-US" style="font-family:宋体; color:black">)[1];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherToday = weatherToday +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">气温:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>+ detail.getProperty(8).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherToday = weatherToday +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">风力:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>+ detail.getProperty(9).toString() +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>iconToday[0] = parseIcon(detail.getProperty(10).toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>iconToday[1] = parseIcon(detail.getProperty(11).toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">解析明天的天气情况</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>date = detail.getProperty(12).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherTomorrow =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"</span><span style="font-family:宋体; color:#2A00FF">明天:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体; color:black"> + date.split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">" "</span><span lang="EN-US" style="font-family:宋体; color:black">)[0];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherTomorrow = weatherTomorrow +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">天气:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体; color:black"> + date.split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">" "</span><span lang="EN-US" style="font-family:宋体; color:black">)[1];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherTomorrow = weatherTomorrow +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">气温:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>+ detail.getProperty(13).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherTomorrow = weatherTomorrow +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">风力:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>+ detail.getProperty(14).toString() +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">解析后天的天气情况</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>date = detail.getProperty(17).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherAfterday =</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"</span><span style="font-family:宋体; color:#2A00FF">后天:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体; color:black"> + date.split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">" "</span><span lang="EN-US" style="font-family:宋体; color:black">)[0];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherAfterday = weatherAfterday +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">天气:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体; color:black"> + date.split(</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">" "</span><span lang="EN-US" style="font-family:宋体; color:black">)[1];</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherAfterday = weatherAfterday +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">气温:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>+ detail.getProperty(18).toString();</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>weatherAfterday = weatherAfterday +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n</span><span style="font-family:宋体; color:#2A00FF">风力:<span lang="EN-US">"</span></span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span>+ detail.getProperty(19).toString() +</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"\n"</span><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>iconAfterday[0] = parseIcon(detail.getProperty(20).toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span>iconAfterday[1] = parseIcon(detail.getProperty(21).toString());</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">更新当天的天气实况</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherCurrent</span><span lang="EN-US" style="font-family:宋体; color:black">.setText(weatherCurrent);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">更新显示今天天气的图标和文本框</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherToday</span><span lang="EN-US" style="font-family:宋体; color:black">.setText(weatherToday);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black">.setImageResource(iconToday[0]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">todayWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black">.setImageResource(iconToday[1]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">更新显示明天天气的图标和文本框</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherTomorrow</span><span lang="EN-US" style="font-family:宋体; color:black">.setText(weatherTomorrow);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black">.setImageResource(iconTomorrow[0]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">tomorrowWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black">.setImageResource(iconTomorrow[1]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">更新显示后天天气的图标和文本框</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">textWeatherAfterday</span><span lang="EN-US" style="font-family:宋体; color:black">.setText(weatherAfterday);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon1</span><span lang="EN-US" style="font-family:宋体; color:black">.setImageResource(iconAfterday[0]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><span lang="EN-US" style="font-family:宋体; color:#0000C0">afterdayWhIcon2</span><span lang="EN-US" style="font-family:宋体; color:black">.setImageResource(iconAfterday[1]);</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体"> </span></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><span lang="EN-US" style="font-family:宋体; color:#3F7F5F">//</span><span style="font-family:宋体; color:#3F7F5F">工具方法,该方法负责把返回的天气图标字符串,转换为程序的图片资源<span lang="EN-US">ID</span>。</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">private</span></strong><span lang="EN-US" style="font-family:宋体; color:black"/><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">int</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> parseIcon(String strIcon)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>{</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (strIcon ==</span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">null</span></strong><span lang="EN-US" style="font-family:宋体; color:black">)</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> -1;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"0.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_0</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"1.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_1</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"2.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_2</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"3.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_3</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"4.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_4</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"5.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_5</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"6.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_6</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"7.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_7</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"8.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_8</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"9.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_9</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"10.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_10</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"11.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_11</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"12.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_12</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"13.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_13</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"14.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_14</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"15.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_15</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"16.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_16</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"17.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_17</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"18.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_18</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"19.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_19</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"20.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span><span style="">   </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_20</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"21.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_21</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"22.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_22</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"23.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_23</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"24.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_24</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"25.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_25</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"26.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_26</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"27.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_27</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"28.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_28</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"29.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_29</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"30.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_30</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">if</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> (</span><span lang="EN-US" style="font-family:宋体; color:#2A00FF">"31.gif"</span><span lang="EN-US" style="font-family:宋体; color:black">.equals(strIcon))</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">           </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> R.drawable.</span><em><span lang="EN-US" style="font-family:宋体; color:#0000C0">a_31</span></em><span lang="EN-US" style="font-family:宋体; color:black">;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">       </span></span><strong><span lang="EN-US" style="font-family:宋体; color:#7F0055">return</span></strong><span lang="EN-US" style="font-family:宋体; color:black"> 0;</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal" align="left" style="text-align:left; text-autospace:none"><span lang="EN-US" style="font-family:宋体; color:black"><span style="">   </span>}</span><span lang="EN-US" style="font-family:宋体"/></p><p class="MsoNormal"><span lang="EN-US" style="font-family:宋体"> </span></p></td></tr></tbody></table> 上面的Activity代码己经不再涉及调用WebService的代码了,只是简单地调用Web Service操作,解析Web Service返回的SOAP消息包,并把SOAP消息包中的数据显示出来。 未完待续..........
';

前言

最后更新于:2022-04-01 09:32:05

> 原文出处:[Android开发高手进阶](http://blog.csdn.net/column/details/androidadvanced.html) 作者:[fengyuzhengfan](http://blog.csdn.net/fengyuzhengfan) **本系列文章经作者授权在看云整理发布,未经作者允许,请勿转载!** # Android开发高手进阶 > 学习是一种态度,分享是一种精神!
';