Я пытался использовать много кодов, которые я нашел для загрузки файлов с AsyncTask, но безуспешно.Я получаю сообщение об ошибке в logcat: E/Error:: No such file or directory
.Несмотря на поиск решений для этой ошибки, не удалось найти Что отсутствует или что не так.
Это метод doInBackground, в котором я предполагаю, что что-то отсутствует / неправильно:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new DownloadJSON().execute("http://api.androidhive.info/json/movies.json");
}
protected String doInBackground(String...fileUrl) {
int count;
try {
String root = "data/data/com.example.jsonapp2";
URL url = new URL(fileUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
File fileName = new File(root+"/movies.json");
boolean existsOrNot = fileName.createNewFile(); // if file already exists will do nothing
// Output stream to write file
OutputStream output = new FileOutputStream(fileName,false);
byte data[] = new byte[1024];
System.out.println("Downloading");
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
Спасибо.
Не хотел бомбардировать избыточным кодом.Если нужен какой-то другой код, я бы с радостью его предоставил.