我正在尝试从Internet下载文件。我已经给了所有许可。但我的应用程序给出了一些例外。这是我的源代码..........
private void write()
{
try {
URL url = new URL("http://wordpress.org/plugins/about/readme.txt");
// URL url = new URL("http://androidsaveitem.appspot.com/downloadjpg");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
// c.setReadTimeout(10000); // millis
// c.setConnectTimeout(15000); // millis
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
File file = new File(PATH);
file.mkdirs();
String fileName = "Sap.txt";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
// }
} catch (IOException e) {
MessageBox(e.getMessage());
}
}
//permission
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
c.connect(); //这个connect()函数给出了一个异常。我无法确定问题所在。
请有人帮助我....
答案 0 :(得分:1)
在主要功能中添加此功能
new AsyncTaskRunner().execute("");
在主要功能
之后添加以下内容private class AsyncTaskRunner extends AsyncTask<String, String, String>
{
@Override
protected void onPostExecute(String result) {
try
{
MessageBox(result);
/*
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
File file = new File(PATH);
file.mkdirs();
String fileName = "Sap.txt";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c;
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
*/
}
catch(Exception ex)
{
MessageBox(ex.getMessage()+"error ");
}
}
@Override
protected String doInBackground(String... params) {
try
{
URL url = new URL("http://wordpress.org/plugins/about/readme.txt");
// URL url = new
// URL("http://androidsaveitem.appspot.com/downloadjpg");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
// c.setReadTimeout(10000); // millis
// c.setConnectTimeout(15000); // millis
//c.setDoOutput(true);
c.connect();
InputStream is = c.getInputStream();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
File file = new File(PATH);
file.mkdirs();
String fileName = "Sap.txt";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
//InputStream is = c;
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Log.v("esty", "Successfully");
return "Successfully";
}
catch(Exception ex)
{
Log.v("esty", ex.getMessage());
return "failed"+ex.getMessage();
}
}
我希望它能解决你的问题! :)