【发布时间】:2016-07-05 13:18:51
【问题描述】:
这让我发疯 - 我在 android 中有一个对象模型,它使用 GsonBuilder 转换为 JSON 字符串:
WorkItemModel model = new WorkItemModel();
model.Jobs = jobList;
model.Items = itemList;
model.Images = imageList;
model.Questions = questionList;
String gsonJson = new GsonBuilder().create().toJson(model);
使用此方法将字符串传递给 c# webservice:
private String callWebService(String JSONModel, URL url, int appId) throws Exception {
int connTimeout = 5000;
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
//Populate Header
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
conn.setConnectTimeout(connTimeout);
conn.setReadTimeout(connTimeout);
if (JSONModel != null) {
conn.setRequestProperty("Content-Length", String.valueOf(JSONModel.length()));
conn.setDoInput(true);
OutputStream stream = new BufferedOutputStream(conn.getOutputStream());
stream.write(JSONModel.getBytes());
stream.flush();
stream.close();
} else {
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
conn.setRequestProperty("Content-Length", "0");
}
conn.connect();
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder returnStringBuilder = new StringBuilder();
String streamString;
while ((streamString = inputStreamReader.readLine()) != null) {
returnStringBuilder.append(streamString);
}
inputStreamReader.close();
return returnStringBuilder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
throw e;
} catch (SocketTimeoutException e) {
throw e;
} catch (SSLHandshakeException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
} finally {
conn.disconnect();
}
return null;
}
网络服务应该接收到这个模型,然后离开并做一些后端数据库的事情——在大多数情况下,我们没有问题,但我们有一些我可以复制的错误报告,但是似乎无法修复。
在我们模型的“工作”列表中,我们有一个名为 completeNotes 的字符串属性。当 completeNotes 用 4 个“£”符号填充时,一切仍然有效,但是当有 5 个“£”符号时,它接收到的 webservice Model 为 NULL,我可以看到 webservice 上抛出了异常:
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Additional information: Invalid character after parsing property name. Expected ':' but got: . Path 'Jobs', line 1, position 1585.
顺便说一句,这也发生在 € 符号上,除了它只允许 2 个 € 符号。在第三个它做同样的事情 - 传递一个 NULL 模型 - 符号不必是连续的要么
我不会将整个 JSON 显示为非常冗长.. 但我所说的属性是这样设置的:
"completeNotes":"£££££"
有没有人知道为什么会发生这种情况 - 我试过弄乱编码(将其设置为 UTF-8),但这似乎没有什么区别。
【问题讨论】:
标签: java c# android json web-services