dotMobimobiThinkingmobiForgemobiReadyDeviceAtlasgoMobi

Posted by pok.lee 1 year 21 weeks ago

pic
 pok.lee
mobiForge Newbie
Posts: 1
Joined: 1 year ago
[offline]

In the java api, there is a method Api.getTreeFromFile(java.lang.String filename) that takes a file path, but I want to load create this tree from a generic inputstream source instead. Why is there no method like Api.getTreeFromInputStream? I tried loading a hashmap using the Json instead, but this doesnt work. Is there a way to do this?

This is what I tried:
1. Read the generic inputstream into a string.
2. Use new Json(String).getHashMap() as the tree

Full example:

InputStream is = null; 
		HashMap tree = null;
		try {
			// This would be another kind of stream, but for demo, its a file stream
			is = new FileInputStream("C:\\20101222.json");
			if (is != null) {
				InputStreamReader reader = new InputStreamReader(is);
				int read;
 
				StringBuffer buf = new StringBuffer(1024);
				while ((read=reader.read()) != -1) {
					buf.append((char)read);
				}
 
				Json json = new Json(buf.toString());
				tree = json.getHashMap();
			}
		} catch (JsonException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			is.close();
		}
 
		// This line blows up
		HashMap props = Api.getProperties(tree, "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; SPH-M910 Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
 
		System.out.println("OK");