Activity HttpURLConnection的简单使用
我想通过HttpURLConnection 请求一个下载地址
并地址服务器会在输出流里面写入 东西, 因此我获取输入流 并将此地址对应的资源文件下载到sdcard当中
以下是我的代码:
//随便找了个歌曲地址URL url = new URL("http://211.162.76.49/download/185049/194841/4/mp3/101/51/1269329382245_563/love1.mp3"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //连接conn.connect(); //获取输入流InputStream stream = conn.getInputStream(); //这里做个测试只获取其中的一部分数据byte[] data=new byte[10240]; //读取int length=stream.read(data); //将次byte转换成StringString str=new String(data,0,length); //断开连接conn.disconnect(); // 输出信息Log.v("luo_wei", str);//关闭流stream.close();