【问题标题】:NULL JSON Model passed to webservice when specific number of special characters当特定数量的特殊字符时,NULL JSON 模型传递给 web 服务
【发布时间】: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


    【解决方案1】:

    您是否尝试将“Charset=UTF-8”添加到内容类型标头?

    conn.setRequestProperty("Content-Type", "application/json; Charset=UTF-8");
    

    【讨论】:

    • 是的。我还强制输出流也用 UTF-8 写入。 stream.write(JSONModel.getBytes(Charset.forName("UTF-8")));没有区别 - 如果要责备字符集,我认为它不会接受一个“£”符号 - 但它接受四个..而不是五个!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多