0

I'm trying to get a JSON string from a file saved under the downloads folder of an Android device. Now I'm able to get a path to this just fine using a filebrowser provided here.

I attempted to use the idion given here to open the file and read out a string. (evidently Android doesn't use Java 7? I am unable to use the answer from this question, the only Files class I find is under android.provider.MediaStore)

In any case, I get (sort of) the JSON string using the idiom from the question. Problem is I can't use the output with GSON (error: Expected BEGIN_OBJECT but was STRING at line 1 column 1)

I suspect it's because what I'm getting back from the file to string function looks like this:

����z������9>{
   "chEqEnable": [
   [
      true,
      true,
... etc ...

Also occasionaly values for say, a boolean looks like:

z������      true,

instead of

true,

Is there some better way when dealing with Android and external storage, to take my file and read a JSON string from it, abberation free?

For reference, here is what my implementation of the idiom from the other question looks like:

public static String file2String(String filePath) throws IOException
{

    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    String line = null;

    try{
        while( (line = reader.readLine()) != null)
        {
            stringBuilder.append(line);
        }
    } finally {
        reader.close();
    }

    String returnStr = stringBuilder.toString();
    return returnStr;
}

When I write a file to external storage I use this

public static String ExportTuning(Context context, String fileName, String json)
    {
        File path;
        if(fileName.contains(".rfts")) {
            path = GetDocumentsPath(fileName);
        } else {
            path = GetDocumentsPath(fileName+".rfts");
        }

        try {
            FileOutputStream fileOut = new FileOutputStream(path);
            ObjectOutput strOut = new ObjectOutputStream(fileOut);
            strOut.writeUTF(json);
//            strOut.writeChars(json); // this just causes a ton of gibberish when I read the file back
            strOut.close();
            fileOut.close();

            String[] paths = new String[]{path.getAbsolutePath()};
            MediaScannerConnection.scanFile(context, paths, null, null);

            return "Save Successful";
        }
        catch(java.io.IOException e) {
            return e.getMessage();
        }
    }
Community
  • 1
  • 1
Magic Marbles
  • 403
  • 2
  • 5
  • 15

1 Answers1

1

Is there some better way when dealing with Android and external storage, to take my file and read a JSON string from it, abberation free?

Your primary problem lies in the ExportTuning() method. You are not writing out JSON. You are writing out a Java object to an ObjectOutputStream. If you want to write JSON, get rid of the ObjectOutputStream and use something that writes text, not objects (e.g., PrintWriter). Please note that you can examine JSON files using your favorite text editor, or cat, or whatever, to see if it contains what you expect.

In terms of parsing the JSON later, delete all your read-into-a-string code, create a Reader object (e.g., an InputStreamReader), and pass that to fromJson().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Finally managed to get the file off the tablet (Never buy an ASUS!) And yeah looks like it's all byte code. I'll give your solution a shot. – Magic Marbles Dec 14 '16 at 22:31