I'm attempting to download a file from the National Weather Service (the third link here), save it as a File in local storage, and then turn the contents of that file into a useable String. However I'm running into an issue where the text from the file is garbled into completely unusable characters and symbols, even though I know for a fact that the actual KML file itself is viewable just fine.
Here's the code I'm using to download the file. I will note that I'm fairly new to Android development and so this code is basically an amalgamation of different tutorials.
Edit: the line below is an example of what I'm seeing when I run this code.
PK�������~�L�]�[��S�������
protected String doInBackground(String... inputUrl) {
int count;
String filepath;
String result = "";
Date currentTime = Calendar.getInstance().getTime();
try{
File file = new File(context.getFilesDir(), "data_" + currentTime);
URL url = new URL(inputUrl[0]);
URLConnection connection = url.openConnection();
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream, 8192);
byte data[] = new byte[1024];
while ((count = inputStream.read(data)) != -1){
outputStream.write(data, 0, count);
}
outputStream.flush();
outputStream.close();
inputStream.close();
filepath = file.getAbsolutePath();
result = getStringFromFile(filepath);
} catch (Exception e){
Log.e("DownloadDataAsync", "Download data failed " + e.toString());
}
return result;
}
The function getStringFromFile()
comes from this question on Stack Overflow.