class Talk {
String[] values;
try {
InputStream is = getAssets().open("jdata.txt");
DataInputStream in = new DataInputStream(is);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line
while ((br.readLine()) != null) {
// Print the content on the console
strLine = strLine + br.readLine();
}
} catch (Exception e) { //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
parse(strLine);
}
public void parse(String jsonLine) {
Data data = new Gson().fromJson(jsonLine, Data.class);
values[0]= data.toString();
return;
}
}
This is in jdata.txt:
"{" + "'users':'john' + "}"
This is my Data.java:
public class Data {
public String users;
}
The error I get is:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 9
Can anyone explain to me what this error means and how to remove it?
EDIT:
I got the answer. These are the tweaks I had to do. First, change the String array to an array list.
List<String> values = new ArrayList<String>();
The next tweak was here:
strLine = currentLine;
currentLine = br.readLine();
//Read File Line By Line
while (currentLine != null) {
// Print the content on the console
strLine = strLine + currentLine;
currentLine = br.readLine();
}
The final tweak was here:
String val = data.toString();
values.add(val);
Certain parts of the code might be redundant but I will take care of that later.
You’re calling
readLine()twice. The following causes a line to be read from the file and lost:Change the loop to:
Also, the contents of
jdata.txtshould be:without superfluous
+or"characters.