天天看点

网络歌曲下载

1、权限

2、代码

name=etmc.gettext().tostring();//获取歌曲自定义名称

byte[]

bname=name.getbytes();

try

{

name=new string(bname,"utf-8");//中文转码

}

catch

(unsupportedencodingexception e1)

e1.printstacktrace();

new

thread()

public void

run()

url url=new

url(uri);//获取链接

httpurlconnection conn = (httpurlconnection)

url.openconnection();//打开链接

conn.setconnecttimeout(6*

1000);//设置时间缓冲

if (conn.getresponsecode() != 200) throw new

runtimeexception("请求url失败");//判断链接是否失败

inputstream is =

conn.getinputstream();//获取inputstream流对象

readasfile(is,new

file("/sdcard/"+name+".mp3"));//将获取inputstream流对象数据存入指定文件夹

catch (exception e)

e.printstacktrace();

}.start();