- 浏览: 430899 次
- 性别:
- 来自: 深圳/湛江
-
文章分类
最新评论
-
wangyudong:
很多API doc生成工具生成doc需要重度依赖代码里加注解的 ...
[ios]利用xcode自动生成项目文档 -
tiger1819:
有源码么,想学习
[android]仿360状态,类流量监控桌面浮动显示 -
119568242:
借我一双翅膀 写道大哥,求指教啊?
IOS 开发,调用打电话,发短信,打开网址 -
借我一双翅膀:
大哥,求指教啊
IOS 开发,调用打电话,发短信,打开网址 -
li247276297:
楼主 是不是要加个权限?
[android]仿360状态,类流量监控桌面浮动显示
http://ask.lurencun.com
海大知道
下载
1. 作用:通过HTTP从互联网下载内容
2. 重点:
1.直接下载内容不保存到Sdcard时 通过url.opneConnection获得连接
2.需要保存成文件时 先判断是否有文件,无则先创建文件夹, 在创建文件。
使用:
不保存到file的
1.通过url获得连接 并强转成HttpURLConnection对象
URL url=new URL(urlstr);
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
2.通过HttpURLConnection对象获取inputStream对象并一步步的装饰成BufferedReader对象。
br=new BufferedReader(new InputStreamReader(hc.getInputStream()));
3.从BufferedReader通过readline()读数据
while((line=br.readLine())!=null){
sb.append(line); //sb是一个StringBuffer对象
}
//bufferReader .readLine() 输出为下一行数据内容且把游标指向下一行 默认游标是在第一行之上 固,每次调用readLine()必须有对象接受此方法返回值(返回值为执行前游标所指的下一行数据)
//Returns the next line of text available from this reader
4.最后把stringbutter 对象toString()生成string对象输出。
保存成file文件的
注意:保存成file的类 应该独立写 只需传入 url,保存的文件名,保存的路径即可。
1.构造file的类 传入当前SD的路径 通过Environment.getExternalStorageDirectory()获得
因为不是所有手机的SD路径都一样 所以这样写可以较好的匹配各种型号的手机
String SDPATH=Environment.getExternalStorageDirectory()
2.判断是否存在当前文件。
public boolean isisFileExist(String path)
{
File file=new File(sd+path);
return file.exists();
}
3.如不存在则生成文件夹 再生成文件
public void creatDirPath(String path)
{
File dir=new File(sd+path);
dir.mkdir();
}
public File creatfile(String namepath) throws IOException
{
File file=new File(sd+namepath);
file.createNewFile();
return file;
}
4.把通过步骤3创造出的FILE对象传入到new FileOutputStram(FILE对象)。 得到一个向File文件输出的FileOutputStram对象。
file=creatfile(dirpath+namepath);
out=new FileOutputStream(file);
5.创建缓冲区
byte buffer [] = new byte[4 * 1024];//缓冲区大小为4K
6.读传入的inputStream流 并写入FileOutputStream
while(is.read(buffer)!=-1){//每次读4K的数据 写入buffer
out.write(buffer);//每次从buffer 取出4k数据
}
7.刷新缓冲区 关闭输出流
刷新缓冲
out.flush();
关闭输出流
finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代码
DownloadActivity
package cfuture.poolo;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cfuture.tool.*;
public class DownloadActivity extends Activity {
private Button txtbn=null;
private Button filebn=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbn=(Button)this.findViewById(R.id.txtbutton);
filebn=(Button)this.findViewById(R.id.filebutton);
System.out.println("1111");
txtbn.setOnClickListener(new txtbn());
filebn.setOnClickListener(new filebn());
}
class txtbn implements OnClickListener
{
public void onClick(View arg0) {
download d=new download();
String temp=d.downloadTxt("http://192.168.1.101:8080/temp/1.txt");
System.out.println(temp);
}
}
class filebn implements OnClickListener
{
public void onClick(View arg0) {
download d=new download();
try {
System.out.println("22222222222");
d.url2file("http://192.168.1.101:8080/temp/1.wav","muisc/","3.wav");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Download.java
package cfuture.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class download {
public String downloadTxt(String urlstr)
{
String line=null;
StringBuffer sb=new StringBuffer();
BufferedReader br=null;
try {
URL url=new URL(urlstr);
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
br=new BufferedReader(new InputStreamReader(hc.getInputStream()));
while((line=br.readLine())!=null){
sb.append(line);
}
//br.readLine() 输出为下一行数据内容且把游标指向下一行 默认游标是在第一行之上
//Returns the next line of text available from this reader
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}
public void url2file(String urlstr,String path,String filename) throws IOException{
FileSD fd=new FileSD();
URL url= new URL(urlstr);
HttpURLConnection ht=(HttpURLConnection)url.openConnection();
int x=fd.write2file(ht.getInputStream(), path, filename);
System.out.println("================="+x);
}
}
FileSD.java
package cfuture.tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Environment;
public class FileSD {
String sd=null;
public String getSd()
{
return this.getSd();
}
public FileSD()
{
sd=Environment.getExternalStorageDirectory()+"/";
}
//创建路径
public void creatDirPath(String path)
{
File dir=new File(sd+path);
dir.mkdir();
}
public File creatfile(String namepath) throws IOException
{
File file=new File(sd+namepath);
file.createNewFile();
return file;
}
public boolean isisFileExist(String path)
{
File file=new File(sd+path);
return file.exists();
}
public int write2file(InputStream is,String dirpath,String namepath)
{
File file=null;
FileOutputStream out=null;
if(isisFileExist(dirpath+namepath)){
return 0;
}else{
creatDirPath(dirpath);
try {
file=creatfile(dirpath+namepath);
out=new FileOutputStream(file);
int x=0;
byte buffer [] = new byte[4 * 1024];//缓冲区大小为4K
while(is.read(buffer)!=-1){//每次读4K的数据 写入buffer
out.write(buffer);//每次从buffer 取出4k数据
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
return -1;
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
}
}
2011-8-14
poolo
海大知道
下载
1. 作用:通过HTTP从互联网下载内容
2. 重点:
1.直接下载内容不保存到Sdcard时 通过url.opneConnection获得连接
2.需要保存成文件时 先判断是否有文件,无则先创建文件夹, 在创建文件。
使用:
不保存到file的
1.通过url获得连接 并强转成HttpURLConnection对象
URL url=new URL(urlstr);
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
2.通过HttpURLConnection对象获取inputStream对象并一步步的装饰成BufferedReader对象。
br=new BufferedReader(new InputStreamReader(hc.getInputStream()));
3.从BufferedReader通过readline()读数据
while((line=br.readLine())!=null){
sb.append(line); //sb是一个StringBuffer对象
}
//bufferReader .readLine() 输出为下一行数据内容且把游标指向下一行 默认游标是在第一行之上 固,每次调用readLine()必须有对象接受此方法返回值(返回值为执行前游标所指的下一行数据)
//Returns the next line of text available from this reader
4.最后把stringbutter 对象toString()生成string对象输出。
保存成file文件的
注意:保存成file的类 应该独立写 只需传入 url,保存的文件名,保存的路径即可。
1.构造file的类 传入当前SD的路径 通过Environment.getExternalStorageDirectory()获得
因为不是所有手机的SD路径都一样 所以这样写可以较好的匹配各种型号的手机
String SDPATH=Environment.getExternalStorageDirectory()
2.判断是否存在当前文件。
public boolean isisFileExist(String path)
{
File file=new File(sd+path);
return file.exists();
}
3.如不存在则生成文件夹 再生成文件
public void creatDirPath(String path)
{
File dir=new File(sd+path);
dir.mkdir();
}
public File creatfile(String namepath) throws IOException
{
File file=new File(sd+namepath);
file.createNewFile();
return file;
}
4.把通过步骤3创造出的FILE对象传入到new FileOutputStram(FILE对象)。 得到一个向File文件输出的FileOutputStram对象。
file=creatfile(dirpath+namepath);
out=new FileOutputStream(file);
5.创建缓冲区
byte buffer [] = new byte[4 * 1024];//缓冲区大小为4K
6.读传入的inputStream流 并写入FileOutputStream
while(is.read(buffer)!=-1){//每次读4K的数据 写入buffer
out.write(buffer);//每次从buffer 取出4k数据
}
7.刷新缓冲区 关闭输出流
刷新缓冲
out.flush();
关闭输出流
finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代码
DownloadActivity
package cfuture.poolo;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cfuture.tool.*;
public class DownloadActivity extends Activity {
private Button txtbn=null;
private Button filebn=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbn=(Button)this.findViewById(R.id.txtbutton);
filebn=(Button)this.findViewById(R.id.filebutton);
System.out.println("1111");
txtbn.setOnClickListener(new txtbn());
filebn.setOnClickListener(new filebn());
}
class txtbn implements OnClickListener
{
public void onClick(View arg0) {
download d=new download();
String temp=d.downloadTxt("http://192.168.1.101:8080/temp/1.txt");
System.out.println(temp);
}
}
class filebn implements OnClickListener
{
public void onClick(View arg0) {
download d=new download();
try {
System.out.println("22222222222");
d.url2file("http://192.168.1.101:8080/temp/1.wav","muisc/","3.wav");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Download.java
package cfuture.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class download {
public String downloadTxt(String urlstr)
{
String line=null;
StringBuffer sb=new StringBuffer();
BufferedReader br=null;
try {
URL url=new URL(urlstr);
HttpURLConnection hc=(HttpURLConnection)url.openConnection();
br=new BufferedReader(new InputStreamReader(hc.getInputStream()));
while((line=br.readLine())!=null){
sb.append(line);
}
//br.readLine() 输出为下一行数据内容且把游标指向下一行 默认游标是在第一行之上
//Returns the next line of text available from this reader
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}
public void url2file(String urlstr,String path,String filename) throws IOException{
FileSD fd=new FileSD();
URL url= new URL(urlstr);
HttpURLConnection ht=(HttpURLConnection)url.openConnection();
int x=fd.write2file(ht.getInputStream(), path, filename);
System.out.println("================="+x);
}
}
FileSD.java
package cfuture.tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Environment;
public class FileSD {
String sd=null;
public String getSd()
{
return this.getSd();
}
public FileSD()
{
sd=Environment.getExternalStorageDirectory()+"/";
}
//创建路径
public void creatDirPath(String path)
{
File dir=new File(sd+path);
dir.mkdir();
}
public File creatfile(String namepath) throws IOException
{
File file=new File(sd+namepath);
file.createNewFile();
return file;
}
public boolean isisFileExist(String path)
{
File file=new File(sd+path);
return file.exists();
}
public int write2file(InputStream is,String dirpath,String namepath)
{
File file=null;
FileOutputStream out=null;
if(isisFileExist(dirpath+namepath)){
return 0;
}else{
creatDirPath(dirpath);
try {
file=creatfile(dirpath+namepath);
out=new FileOutputStream(file);
int x=0;
byte buffer [] = new byte[4 * 1024];//缓冲区大小为4K
while(is.read(buffer)!=-1){//每次读4K的数据 写入buffer
out.write(buffer);//每次从buffer 取出4k数据
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
return -1;
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 1;
}
}
}

2011-8-14
poolo
发表评论
-
[android]使用 Matrix 的随触摸旋转的ImageView
2013-02-22 01:58 7955使用 Matrix 的随触摸旋转的ImageView 突 ... -
[android]动态改变按钮背景状态 StateListDrawable
2012-10-29 10:52 1491动态改变按钮背景状态 很少用到 上次和六哥聊到。 ... -
[android]ViewPage上无法通过onKeyDown()获得按钮事件
2012-08-20 04:53 1397系统无法监听到遥控器在VewPage上的按钮事件,如需 ... -
[android]待解决 lisTview 的onItemSelected 监听事件焦点的问题。
2012-08-18 21:18 2198今天遇到个问题 到目前为止 一直不理解为什么 大概描述下布局 ... -
[android]layout_weight 在layout_width 为fill_parent 与wrap_content 时的不同含义
2012-08-12 12:52 1279转自:http://hi.baidu.com/l ... -
[android]仿制新浪微博消息页面 图标切换动画
2012-08-10 17:33 4062研究了下以前不怎么用到的动画效果的实现 顺便做了一个新浪微 ... -
[android]仿制新浪微博消息页面 图标切换动画
2012-08-10 17:33 3研究了下以前不怎么用到的动画效果的实现 顺便做了一个新浪微 ... -
[android]仿制新浪微博消息页面 图标切换动画
2012-08-10 17:33 0研究了下以前不怎么用到的动画效果的实现 顺便做了一个新浪微 ... -
[android]Activity切换动画
2012-08-10 12:23 1493今天准备比赛的时候 遇到了这个问题。 查了些资料 ... -
android中ADT版本问题:无故报 java.lang.NoClassDefFoundError
2012-07-31 22:08 1818今天修改一个老项目的时候,发现在所有配置正确的情况下,代目无任 ... -
[android]仿360状态,类流量监控桌面浮动显示
2012-05-27 22:03 6064前两天看到部分音频播放器可以实现在桌面上显示歌词,360那个浮 ... -
[转]android开发问题记录 "founderapp"
2012-02-08 10:48 1087这段时间,由于某种原因,一直在做android手机开发, ... -
关于weight
2012-02-05 21:20 858layout_weight=1后,除了其它的控件之外剩 ... -
判断SD卡是否存在
2012-02-02 17:54 901android.os.Environment.getEx ... -
[转]Android文件管理器介绍
2012-02-02 16:50 3096转自:http://www.linuxidc.com/L ... -
[转]Android文件管理器介绍
2012-02-02 16:49 5转自:http://www.linuxidc.com/L ... -
[转]android 几何图形的绘制
2012-02-01 16:06 2211转自:http://byandby.iteye.c ... -
Android菜鸟日记32-游戏中的碰撞
2012-01-11 23:09 1768Android菜鸟日记 32-游戏中的碰撞 一、 ... -
Android菜鸟日记31-selector 中使用 shape
2012-01-11 23:05 1643Android菜鸟日记 31- ... -
Android菜鸟日记30-View与SurfaceView
2012-01-11 22:45 972Android菜鸟日记 30 View与Surfa ...
相关推荐
这篇“Android菜鸟日记25-android反编译”将带你走进Android反编译的世界,揭示APK背后的秘密。 首先,让我们了解什么是Android反编译。Android应用主要由Java语言编写,经过编译后生成Dalvik字节码(.dex文件),...
《构建私密日记本:Android小程序开发详解》 在当今数字化时代,个人隐私的保护越来越受到重视,而私密日记本作为一个记录内心世界的私密空间,其数字化形式——Android小程序,成为了许多用户的新选择。本文将详细...
在“个人日记本”中,数据存储可能采用了常见的数据库技术,如SQLite,它是Android系统内置的轻量级数据库,适合小型应用的数据存储需求。SQLite支持SQL语言,用户可以通过创建表来保存日记条目,包括日期、标题、...
记事本程序源码主要涉及的技术点就是SQLite,源码不大注释得体,并且附带项目报告一份。是安卓菜鸟拿来练手的绝好程序。日记的数据主要记录在本地的SQLite数据库中,程序里面有个日志搜索功能,不过没用模糊查询。