Android Load Json from file Error - java

I am building am app the will work offline.
I download the json to a file in the sdcard. then when I try and load it using gson and map it with POJO class I get error:
read the file:
File newList = new File(rootFolder.getParent(), "custom_list.new.dat");
String customListStr = Utils.readTextFile(newList);
public static String readTextFile(File file) throws IOException {
FileInputStream reader = new FileInputStream(file);
StringBuffer data = new StringBuffer("");
byte[] buffer = new byte[1024];
while (reader.read(buffer) != -1)
data.append(new String(buffer));
reader.close();
return data.toString();
}
try to load it
//Load json file to Model
Gson gson = new GsonBuilder().create();
final MoviePojo response = gson.fromJson(customListStr, MoviePojo.class);
int size = response.getContentList().size();
: Error: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 3205 path $

The error shows you, in the text, that the JSON you have downloaded has an error in its structure MalformedJsonException, "Malformed". Check the JSON file, from top to bottom, may be some of this ("{", "[", ",", ":" ) characters are missing or has more than the quantity it needs.
Good luck.

Related

java unknown protocol: e when downloading a file

I'm a beginner to java file handling. I tired to get a bin file (en-parser-chunking.bin) from my hard disk partition to my web application. So far I have tried below code and it gives me the output in my console below.
unknown protocol: e
these are the code samples I have tried so far
//download file
public void download(String url, File destination) throws IOException {
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
public void parserAction() throws Exception {
//InputStream is = new FileInputStream("en-parser-chunking.bin");
File modelFile = new File("en-parser-chunking.bin");
if (!modelFile.exists()) {
System.out.println("Downloading model.");
download("E:\\Final Project\\Softwares and tools\\en-parser-chunking.bin", modelFile);
}
ParserModel model = new ParserModel(modelFile);
Parser parser = ParserFactory.create(model);
Parse topParses[] = ParserTool.parseLine(line, parser, 1);
for (Parse p : topParses){
//p.show();
getNounPhrases(p);
}
}
getting a file in this way is possible or I have done it wrong ?
note - I need to get this from my hard disk. not download from the internet
the correct URL for a local file is:
file://E:/Final Project/Softwares and tools/en-parser-chunking.bin
where file is the protocol.
You can also you:
new File("E:/Final Project/Softwares and tools/en-parser-chunking.bin").toURL()
to create a URL from your file.
I also recomment to use slash as file seperator instead of backslash

IO Exception java.io.FileNotFoundException: (File name too long)

I get a IO Exception java.io.FileNotFoundException:(Filename too long) when I use a BufferedReader to read a file from a url(response is the url response).
String payload = response.readAsString();
try(FileReader reader = new FileReader(payload);
BufferedReader bufferedReader = new BufferedReader(reader)) {
The problem seems to be that the contents of the file is read as the file name and it's longer than what's allowed.
To get around this I've used PrintWriter to write the contents to a file and am reading that file but would like to know if there's a better way to do this.
I went down a different route in the end.
String payload = response.readAsString();
try{
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(payload);
JsonNode vaultsNode = root.path("responseData").path("vaults");
...

Android Studio can't find my JSON file

I created a raw Android resource directory in res and added cars.json to it. It shows up on my package explorer. When I try to call
File file = new File("raw/cars.json")
I get a FileNotFoundException. I've tried using simply cars.json and even the entire path to the file but I get the same results.
Any suggestions?
To get a resource from the raw directory call getResources().openRawResource(resourceName).
This will give you a InputStream
You can use this function directly:
private String getJsonStringFromRaw(filenmae)
{
String jsonString = null;
try {
InputStream inputStream= getAssets().open(filename);
int size = inputStream.available();
byte[] buffer= new byte[size];
inputStream.read(buffer);
inputStream.close();
jsonString = new String(buffer, "UTF-8"); }
catch (IOException e)
{
e.printStackTrace();
return null;
}
return jsonString;
}
You can access json data by iterating through the json object below. JSONObject obj = new JSONObject(getJsonStringFronRaw("cars.json"))
Retrieving resource from the raw directory can be achieved with openRawResources a sub class of getResources, this takes in a resource id (your json file) and returns a InputStream.
InputStream myInput=getResources().openRawResource(R.raw.your_json);
If you need more info, here is the google documention

Apache Tika extracting metadata via jar but not in sample code

I've been able to extract metadata via the tika-app executable jar using the following line:
java -jar tika-app-1.13.jar --metadata example_received_regular.msg
It prints out all of the metadata. But when I try to execute a simple extraction of the same file in a Java program, I don't get any of it.
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("org.apache.tika.parser.ocr.TesseractOCRParser");
FileInputStream des = new FileInputStream("/Users/jason/docstore/example_received_regular.msg");
Tika tika = new Tika();
AutoDetectParser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(-1);
String detected = tika.detect(des);
Metadata tikaMetadata = new Metadata();
parser.parse(des, handler, tikaMetadata, new ParseContext());
String[] names = tikaMetadata.names();
for (String name : names) {
System.out.println(name + ": " + tikaMetadata.get(name));
}
System.out.println(detected);
}
My first thought was that the tika-parser library was somehow unavailable at runtime, hence me attempting to load the TesseractOCRParser on the first line, but that class loads just fine. Executing this program results in the following output:
X-Parsed-By: org.apache.tika.parser.EmptyParser
Content-Type: application/octet-stream
application/x-tika-msoffice
This seems like the most basic example of Tika metadata extraction I can find anywhere. The extraction runs fine with the jar but not in this example. Am I missing something?
The TikaCLI program utilizes a special TikaInputStream object which populates the metadata (unlike the FileInputStream in your example above).
You can make the following changes in order print the metadata values:
public static void main(String[] args) throws Exception {
File file = new File("/Users/jason/docstore/example_received_regular.msg");
AutoDetectParser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(-1);
Metadata tikaMetadata = new Metadata();
InputStream input = TikaInputStream.get(file, tikaMetadata);
parser.parse(input, handler, tikaMetadata, new ParseContext());
String[] names = tikaMetadata.names();
Arrays.sort(names);
for (String name : names) {
System.out.println(name + ": " + tikaMetadata.get(name));
}
}

Extraneous data when reading/writing binary data

I am trying to write a process that will retrieve a file (various types - pdf, txt, docx, tif, etc.) via a rest API call, convert that file's binary data from base64 encoding to un-encoded, and write the file to another location (to be picked up by another process). All of the above is working, but if the file type is anything other than txt, the newly written out file will not open.
public File retrieveDocument(String in_ItemId, File in_DestinationFile, Map<String, String> in_DocumentProperties)
throws IOException {
byte[] binaryData = new byte[8198];
try {
String url = "filestore url";
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
Map documentMap = restTemplate.getForObject(url, Map.class);
if (documentMap.get("binaryData") != null) {
binaryData = Base64.decodeBase64(((String) documentMap.get("binaryData")).getBytes());
}
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(in_DestinationFile));
outputStream.write(binaryData);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return in_DestinationFile;
}
When I open both the original and new files in a text editor (i.e., Notepad++) and compare the two, there are a number of additional characters (mainly question marks) in the new file. Below is an example from a tif image. I've added ^ under some of the additional characters in the new file.
Original file:
II* P €?à#$
„BaP¸d6ˆDbQ8¤V-ŒFcQ¸äv=HdR9$–M'”JeR¹d¶]/˜LfS9¤Öm7œNgS¹äö}? PhT:%3Ñ©Tºe6O¨
‡ÄbqX¼f7•ß²<¦W-—ÌfsY¼æw=ŸÐhlÐ=—M§ÔjuZ½f·]¯Øll™-–×m·Ünw[½æ÷}¿à_tœ'Çäry\¾g7hÚsú]
New file:
II* P €?à#$
„BaP¸d6ˆDbQ8¤V-ŒFcQ¸äv=?HdR9$–M'”JeR¹d¶]/˜LfS9¤Öm7œNgS¹äö}? PhT:%3Ñ©Tºe6?O¨
^ ^
‡ÄbqX¼f7?•ß²<¦W-—ÌfsY¼æw=ŸÐhlÐ=—M§ÔjuZ½f·]¯Øll™-–×m·Ünw[½æ÷}¿à_tœ'?Çäry\¾g7?hÚsú]
^ ^
Any ideas as to what I'm doing wrong?
Writer classes including PrintWriter are for text data. Stream classes such as OutputStream are for binary data.
You're converting binary data into a String at which point some binary data can get corrupted.
Get rid of the String strBinaryData and just the byte[] you get from Base64.decodeBase64.

Categories

Resources