1

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.

CthuluHoop
  • 136
  • 2
  • 15
  • At what point are you looking at result which you say is garbled - is it in 'onPostExecute' or caller of asynctask. The posted code works as standalone java. –  Apr 10 '18 at 02:45
  • After executing `getStringFromFile()`, the resulting string consists of nothing but unusable text and symbols. It would be `onPostExecute` at which point I'm seeing the result looking like this, although in debugging I can see that each line of the file when it is being read into the string is showing up as these characters. I'll update the original question with an example line of what I'm seeing. – CthuluHoop Apr 10 '18 at 15:53
  • Seems like an encoding issue but the file is UTF-8 and the default stream type is also UTF-8 so doesn't seem like that is the issue. –  Apr 10 '18 at 15:58
  • I was thinking that as well at first. Added the first line of the result string in my question as an example of what I'm seeing. – CthuluHoop Apr 10 '18 at 16:04
  • That's interesting - PK (first 2 char) is a file signature of a PKZip file - maybe just coincidence. –  Apr 10 '18 at 16:12
  • You sure you're not grabbing the second link (which is a kmz file) which is compressed. –  Apr 10 '18 at 16:13

1 Answers1

1

I'd guess you are grabbing the KMZ file (the second link on reference NWS page) by mistake. KMZ files are PKzip compressed and have a file signature of "PK" as the first 2 character bytes. The file in second link has this as the first bytes:

00000000h: 50 4B 03 04 14 00 02 00 08 00 45 81 8A 4C F5 BA ; PK........EŠLõº
00000010h: 5E 9E CD 06 02 00 13 95 07 00 07 00 1C 00 77 77 ; ^žÍ....•......ww

which if you attempted to read as a string would result in what is posted.

  • I can't believe I spent so much time on this only to be downloading the wrong file. This is correct, of course! – CthuluHoop Apr 10 '18 at 20:40