终于搞出一个稳定的版本了……
2014-03-24 03:23
211 查看
bug大致都清掉了……
至少第一次运行是不会有bug了。
等会看能不能发github上去,然后直接外链到这边吧。
我花了一个周,就搞出了下载文件这么一个功能……
编程太难玩了。
目前就这样
至少第一次运行是不会有bug了。
等会看能不能发github上去,然后直接外链到这边吧。
我花了一个周,就搞出了下载文件这么一个功能……
编程太难玩了。
package com.mlxy.xml; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import android.content.Context; import android.os.Environment; import com.mlxy.util.CharacterProcesser; /** * XML文件下载器。 * * @author mlxy * */ public class XmlDownloader { private Context parent; private String city = CharacterProcesser.encodeByGB2312("南昌"); private String password = "DJOYnieT8234jlsK"; private int day = 0; private String link = "http://php.weather.sina.com.cn/xml.php?city=" + city + "&password=" + password + "&day=" + day; public XmlDownloader(Context parent) { this.parent = parent; } /** 外部接口,根据设置好的类属性下载xml文件。*/ public void download() { downloadAndVerifyXml(this.link); } /** 根据给定的链接下载对应的xml文件。 * * @param link API的链接,需要标明网络协议。*/ private void downloadAndVerifyXml(String link) { // 用链接字符串new出URL对象 URL url = null; try { url = new URL(link); } catch (MalformedURLException e) { e.printStackTrace(); } // 获取外部存储路径并创建文件对象。 File externalDirectory = Environment.getExternalStorageDirectory(); String fileName = "xml_resource.xml"; File file = new File(externalDirectory, fileName); // 写文件。 // 初始化io流。 InputStream in = null; BufferedReader reader = null; FileOutputStream out = null; BufferedWriter writer = null; try { // 建立连接并给io流赋值。 in = (InputStream) url.getContent(); reader = new BufferedReader(new InputStreamReader(in, "iso-8859-1")); out = this.parent.openFileOutput(file.getName(), Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(out, "iso-8859-1")); // 读一行写一行,然后另起一行。 String line = null; while ((line = reader.readLine()) != null) { writer.write(line); writer.newLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { // 释放资源 writer.flush(); writer.close(); out.close(); reader.close(); in.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public XmlDownloader setCity(String city) { this.city = city; return this; } public String getCity() { return this.city; } public XmlDownloader setPassword(String password) { this.password = password; return this; } public String getPassword() { return this.password; } public XmlDownloader setDay(int day) { this.day = day; return this; } public int getDay() { return this.day; } /** 获取API链接。*/ public String getLink() { return this.link; } }
目前就这样
相关文章推荐
- 终于有了一个稳定的开发环境
- PHP-发布一个composer包(稳定版本)
- github发布一个稳定版本packagist的composer包
- 花了很长的时间终于完成了1.0的稳定版本
- IE9:继IE5以来,微软终于又出了一个给力版本
- redis一个比较大的改变,redis3.0.1 稳定版本发布,支持集群。redis 集群配置 reids Cluster
- redis终于有比较大的进展了,redis3.0.1 稳定版本发布,支持集群。
- xcode创建一个工程的多个taget,便于测试和发布多个版本
- 排序算法不稳定(快排)在一个待排序队列中,A和B相等,且A排在B的前面,而排序之后,A排在了B的后面.
- hibernate继承映射[映射单独一个tablexml版本]实例【与19相同,方式不同,未验证】(二十)
- JDBC的第一个版本,不使用配置文件。使用jar文件一个
- 一个简单的iTextSharp的ASP.NET的例子(根据iText的jsp版本转换而来)
- 电脑安装多个版本SQLServer,卸载其中一个然后重新安装解决办法
- 一个男人在外工作20年,终于要回家了,老板问他:你是要20年的工资还是要3句忠告
- 一个简单的小例子让你明白c#中的委托-终于懂了!
- 用一个文档保存多个版本
- 利用ffmpeg和SDL实现一个跨android版本的音视频播放器
- 一个C++版本的base64编码/解码
- Windows Server "Longhorn" beta 3 版本终于现身
- 因为此计算机没有安装 Windows 的正确版本,它不能成为一个群集的成员。