终于搞出一个稳定的版本了……

bug大致都清掉了……

至少第一次运行是不会有bug了。

等会看能不能发github上去,然后直接外链到这边吧。

我花了一个周,就搞出了下载文件这么一个功能……

编程太难玩了。

  1 package com.mlxy.xml;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.File;
  6 import java.io.FileOutputStream;
  7 import java.io.InputStream;
  8 import java.io.InputStreamReader;
  9 import java.io.OutputStreamWriter;
 10 import java.net.MalformedURLException;
 11 import java.net.URL;
 12 
 13 import android.content.Context;
 14 import android.os.Environment;
 15 
 16 import com.mlxy.util.CharacterProcesser;
 17 
 18 /** 
 19  * XML文件下载器。
 20  * 
 21  * @author mlxy
 22  * */
 23 public class XmlDownloader {
 24     private Context parent;
 25     
 26     private String city = CharacterProcesser.encodeByGB2312("南昌");
 27     private String password = "DJOYnieT8234jlsK";
 28     private int day = 0;
 29     private String link = "http://php.weather.sina.com.cn/xml.php?city=" + city
 30             + "&password=" + password + "&day=" + day;
 31     
 32     public XmlDownloader(Context parent) {
 33         this.parent = parent;
 34     }
 35     
 36     /** 外部接口,根据设置好的类属性下载xml文件。*/
 37     public void download() {
 38         downloadAndVerifyXml(this.link);
 39     }
 40     
 41     /** 根据给定的链接下载对应的xml文件。
 42      * 
 43      * @param link API的链接,需要标明网络协议。*/
 44     private void downloadAndVerifyXml(String link) {
 45         // 用链接字符串new出URL对象
 46         URL url = null;
 47         try {
 48             url = new URL(link);
 49         } catch (MalformedURLException e) {
 50             e.printStackTrace();
 51         }
 52 
 53         // 获取外部存储路径并创建文件对象。
 54         File externalDirectory = Environment.getExternalStorageDirectory();
 55         String fileName = "xml_resource.xml";
 56         File file = new File(externalDirectory, fileName);
 57 
 58         // 写文件。
 59         
 60         // 初始化io流。
 61         InputStream in = null;
 62         BufferedReader reader = null;
 63         FileOutputStream out = null;
 64         BufferedWriter writer = null;
 65         
 66         try {
 67             
 68             // 建立连接并给io流赋值。
 69             in = (InputStream) url.getContent();
 70             reader = new BufferedReader(new InputStreamReader(in, "iso-8859-1"));
 71             out = this.parent.openFileOutput(file.getName(), Context.MODE_PRIVATE);
 72             writer = new BufferedWriter(new OutputStreamWriter(out, "iso-8859-1"));
 73             
 74             // 读一行写一行,然后另起一行。
 75             String line = null;
 76             while ((line = reader.readLine()) != null) {
 77                 writer.write(line);
 78                 writer.newLine();
 79             }
 80             
 81         } catch (Exception e) {
 82             e.printStackTrace();
 83         } finally {
 84             try {
 85                 // 释放资源
 86                 writer.flush();
 87                 writer.close();
 88                 out.close();
 89                 reader.close();
 90                 in.close();
 91             } catch (Exception e2) {
 92                 e2.printStackTrace();
 93             }
 94         }
 95     }
 96     
 97     public XmlDownloader setCity(String city) {
 98         this.city = city;
 99         return this;
100     }
101     public String getCity() {
102         return this.city;
103     }
104     
105     public XmlDownloader setPassword(String password) {
106         this.password = password;
107         return this;
108     }
109     public String getPassword() {
110         return this.password;
111     }
112     
113     public XmlDownloader setDay(int day) {
114         this.day = day;
115         return this;
116     }
117     public int getDay() {
118         return this.day;
119     }
120     
121     /** 获取API链接。*/
122     public String getLink() {
123         return this.link;
124     }
125 }
目前就这样
原文地址:https://www.cnblogs.com/chihane/p/3620070.html