想要 在Android 裡使用編碼過的json。由於 JSONObject 的 toString() 是沒編碼過的,直接放到 URL 或 header 裡,送到 server side 不知道為什麼解不開來, 只好先在 json 裡encode 成 ascii.
官方網站:
https://github.com/FasterXML/jackson-databind
You should enable the JsonGenerator feature which controls the escaping of the non-ASCII characters. Here is an example:
ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
ObjectNode node = mapper.getNodeFactory().objectNode();
node.put("field1", "Maël Hörz");
System.out.println(mapper.writeValueAsString(node));
The output is:
{"field1":"Ma\u00EBl H\u00F6rz"}
arg build-in:{“path”:”\/中文字\/abc\/IMG_20170224_211551.jpg“,”client_modified”:1487942151,”platform”:”android”,”device_id”:”b131f953c8fa34c3″,”device_name”:”MI 3W”,”client_version”:”1.0″,”platform_version”:”6.0.1″,”language”:”en_US”}
arg jackson:{“path”:”/\u4E2D\u6587\u5B57/abc/IMG_20170224_211551.jpg“,”client_modified”:1487942151}
加入到 Android Stuiod:
compile 'com.fasterxml.jackson.core:jackson-core:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
Following android studio conventions. You can find the up to date version at: Maven Central Repository.
Build 起來會遇到 Error:
“Duplicate files copied in APK META-INF/*”
解法:
Add following into respective build.gradle file
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/MANIFEST.MF'
}