2012-12-14 86 views
0

像标题状态我只是试图下载test.txt文件,下面的URL并保存在内部,理想情况下在可绘制内。Android,下载一个.txt文件,并保存在内部

我一直在试图修改这个工作,但会收效甚微我不断收到“无法下载空”的错误

int count;   
try { 
    URL url = new URL("https://www.darkliteempire.gaming.multiplay.co.uk/testdownload.txt"); 
    URLConnection conexion = url.openConnection(); 
    conexion.connect(); 
    int lenghtOfFile = conexion.getContentLength(); 
    InputStream is = url.openStream(); 
    File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Download"); 

    if (!testDirectory.exists()) { 
     testDirectory.mkdir(); 
    } 

    FileOutputStream fos = new FileOutputStream(testDirectory + "/test.txt"); 
    byte data[] = new byte[1024]; 
    long total = 0; 
    int progress = 0; 

    while ((count = is.read(data)) != -1) { 
     total += count; 

     int progress_temp = (int) total * 100/lenghtOfFile; 

     fos.write(data, 0, count); 
    } 

    is.close(); 
    fos.close(); 
} catch (Exception e) { 
    Log.e("ERROR DOWNLOADING", "Unable to download" + e.getMessage()); 
} 

必须有这样做一个简单的方法? 该文件本身是微小的,也许3或4行文本,所以我不需要任何花哨

+1

您的网址错误“https://www.http://dark ...” – fredcrs

+0

'https://www.http://'? – njzk2

回答

0

使用AQuery library你得到了一些非常简单。另外,你会得到其他很酷的功能臀部缩短你的代码。

http://code.google.com/p/android-query/wiki/AsyncAPI

String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=16";    

File ext = Environment.getExternalStorageDirectory(); 
File target = new File(ext, "aquery/myfolder/photos.xml");    
aq.progress(R.id.progress).download(url, target, new AjaxCallback<File>(){    
     public void callback(String url, File file, AjaxStatus status) {      
       if(file != null){ 
         showResult("File:" + file.length() + ":" + file, status); 
       }else{ 
         showResult("Failed", status); 
       } 
     }    
}); 
1

请更新你的下面的代码行,写有效的URL。

URL url = new URL("https://www.http://darkliteempire.gaming.multiplay.co.uk/testdownload.txt"); 

写完有效的url后你的代码行看起来像这样。

URL url = new URL("http://www.darkliteempire.gaming.multiplay.co.uk/testdownload.txt"); 

它会解决你的问题。

+0

是的,我注意到错误后把它放在这里,我纠正它,我仍然有同样的问题 –

+0

@ryan你会得到什么错误? –

相关问题