Android 从本地服务器下载文件与解析过程详解(二)

mp3_info_item.xml:  用于显示相应的信息

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android=http://schemas.android.com/apk/res/android
  4. android:orientation="horizontal"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:paddingLeft="10px"
  8. android:paddingRight="10px"
  9. android:paddingTop="1px"
  10. android:paddingBottom="1px"
  11. >
  12. <TextView
  13. android:id="@+id/mp3_name"
  14. android:layout_width="180dip"
  15. android:layout_height="wrap_content"
  16. android:textSize="10pt"/>
  17. <TextView
  18. android:id="@+id/mp3_size"
  19. android:layout_width="180dip"
  20. android:layout_height="wrap_content"
  21. android:textSize="10pt"
  22. />
  23. </LinearLayout>
复制代码

       (2) 、资源文件: values\strings.xml

java代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, Mp3ListActivity!</string>
  4. <string name="app_name">mp3player</string>
  5. <string name="mp3list_update">更新列表</string>
  6. <string name="mp3list_about">关于</string>
  7. </resources>
复制代码

        (3)、主要逻辑代码:
        1、用于下载本地文件的代码:

java代码:
  1. package eoe.demo;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;

  9. public class HttpDownloader {
  10. private URL url=null;

  11. public String download(String urlStr){
  12. StringBuffer sb=new StringBuffer();
  13. String line=null;
  14. BufferedReader buffer=null;
  15. try{
  16. url=new URL(urlStr);
  17. HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
  18. buffer=new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
  19. while((line=buffer.readLine())!=null){
  20. sb.append(line);
  21. }
  22. }catch(Exception e){
  23. e.printStackTrace();
  24. }finally{
  25. try{
  26. buffer.close();
  27. }catch(Exception e){
  28. e.printStackTrace();
  29. }
  30. }
  31. return sb.toString();
  32. }
  33. /*
  34. * 该函数返回整形:-1 表示下载文件出错 0 表示下载文件成功 1 表示下载文件已经存在
  35. */
  36. /* public int downFile(String urlStr,String path,String fileName){
  37. InputStream inputStream=null;
  38. try{
  39. FileUtils fileUtils=new FileUtils();
  40. if(fileUtils.isFileExist(path+fileName)){
  41. return 1;
  42. }else{
  43. inputStream=getInputStreamFromUrl(urlStr);
  44. File resultFile=fileUtils.write2SDFromInput(path, fileName, inputStream);
  45. if(resultFile==null){
  46. return -1;
  47. }
  48. }
  49. }catch(Exception e){
  50. e.printStackTrace();
  51. return -1;
  52. }finally{
  53. try {
  54. inputStream.close();
  55. } catch (IOException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59. }
  60. return 0;
  61. }

  62. public InputStream getInputStreamFromUrl(String urlStr) throws IOException{
  63. url=new URL(urlStr);
  64. HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
  65. InputStream inputStream=urlConn.getInputStream();
  66. return inputStream;
  67. }*/
  68. }
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值