Andriod从网络上读入文件
来源:互联网 发布:js获取网页源代码 编辑:程序博客网 时间:2024/06/08 07:11
class TxtDownLoadOnClickListener implements View.OnClickListener{ @Override public void onClick(View view) { //TxtDown txtDown = new TxtDown(); //String string = txtDown.downLoad("http://dampce032.iteye.com/blog/975642"); //System.out.println(string); new Thread(){ @Override public void run() { TxtDown txtDown = new TxtDown(); String string = txtDown.downLoad("http://dampce032.iteye.com/blog/975642"); System.out.println(string); super.run(); } }.start(); } }
看的是Mars老师的Andriod教程, 因为教程比较早, 在文件下载的时候 出现
NullPointerException
异常, 崩掉程序。在Andriod4.0以后, 对网络的访问不能写在主线程中了。 可以在监听器中设置线程。网络的读写要用到IO流,下面代码可以实现对string类型的读入。在Mars老师讲解时,网站用Tomcat模拟,我没弄这个,但是也没有找到TXT文件的网站,所以就直接找的随机一个网站读入, 能成功读入。听别人说用PHPStudy也能实现这个功能。
public class TxtDown { private URL url = null; //urlString是下载地址 public String downLoad(String urlString){ StringBuffer stringBuffer = new StringBuffer(); BufferedReader bufferedReader = null; String line = null; try { //初始化URL对象 url = new URL(urlString); // 创建http连接 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); //用IO流读取数据 bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); while ((line = bufferedReader.readLine()) != null){ stringBuffer.append(line); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } return stringBuffer.toString(); }}
另外又写了一个读入网络图片的程序, 和上面的基本上差不多,如果可能的话我再试试能不能封装成一个方法。
class JPGOnClickListener implements View.OnClickListener{ @Override public void onClick(View view) { //DownJPG downJPG = new DownJPG(); //downJPG.GetJPG("http://f.hiphotos.baidu.com/image/w%3D2048/sign=3b06d28fc91349547e1eef6462769358/d000baa1cd11728b22c9e62ccafcc3cec2fd2cd3.jpg"); //System.out.println("OnClick"); new Thread(){ @Override public void run() { DownJPG downJPG = new DownJPG(); downJPG.GetJPG("http://f.hiphotos.baidu.com/image/w%3D2048/sign=3b06d28fc91349547e1eef6462769358/d000baa1cd11728b22c9e62ccafcc3cec2fd2cd3.jpg"); System.out.println("OnClick"); super.run(); } }.start(); } }
public class DownJPG { protected URL url = null; Bitmap bitmap = null; public Bitmap GetJPG(String urlJPG){ try { url = new URL(urlJPG); //使用HttpURLConnect建立连接 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true);//??以后就可以使用conn.getInputStream().read() httpURLConnection.connect(); //get数据 InputStream inputStream = httpURLConnection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); System.out.println("DownLoad"); } catch (MalformedURLException e) { // TODO Auto-generated catch block System.out.println("[getNetWorkBitmap->]MalformedURLException"); e.printStackTrace(); } catch (Exception e) { System.out.println("[getNetWorkBitmap->]IOException"); e.printStackTrace(); } return bitmap; }}
在AndriodManifest.xml文件里面要加入访问网络的权限
<!-- 访问网络的权限 --><uses-permission android:name="android.permission.INTERNET"></uses-permission>
阅读全文
0 0
- Andriod从网络上读入文件
- 从文件读入数据
- 从网络上download文件
- matlab 逐行从文件读入
- 从文件中读入数据
- 关于Andriod读入CVS或者TXT文件的有关问题
- c#从键盘上读入数据
- 从文件读入数据&向文件输出
- 判断读入数据文件结尾【从文件读入数据】
- 要从文件中读入文本
- [C] 从文件读入数据并输出
- ReadFile-从硬盘中读入文件
- python从文件中读入中文字符
- [QT]从文件读入中文字符
- Fortran从文件读入数据的规律
- freopen读入/写出 数据 从/到 文件
- Unity3D 从 TEXT 文件里读入数组
- C语言:从文件中读入数据
- Android实战一:更换头像
- 草根学Python(七) 迭代器和生成器
- 一个基于xposed和inline hook的一代壳脱壳工具
- 组织机构同步和人员同步
- java多线程协作(使用Condition)
- Andriod从网络上读入文件
- Kotlin-28.类型检查和转换(Type Cast)
- ES简介
- 组合模式
- 类里含有静态初始化对象的执行过程和顺序
- 位运算
- mac下安装python-lxml
- JS框架 获取页面传递过来的参数
- Itext笔记之pdf页眉页脚