gpt4 book ai didi

从 Internet 下载的 android 文件出现异常

转载 作者:行者123 更新时间:2023-11-30 02:57:28 25 4
gpt4 key购买 nike

我正在尝试从 Internet 下载文件。我已授予所有权限。但我的应用程序给出了一些异常(exception)。这是我的源代码............

  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() 函数给出了一个异常。我无法确定问题所在。

请有人帮助我......

最佳答案

在main函数中添加

new AsyncTaskRunner().execute("");

在main函数后面添加

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();
}
}

希望能解决你的问题! :)

关于从 Internet 下载的 android 文件出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com