0

通过传递字符串创建 JSONObject 时出现错误。

Org.json.JSONException: Unterminated string at character

我的 JSON 字符串是:

{"Count":741,"Data":[{rec1....},{rec2...}, etc]}

这似乎只发生在我在 Windows 中开发的 Linux 上,并且运行良好。此外,问题似乎源于字符串太长。我将阵列削减了一半以上,问题就消失了。

我可以做些什么来解决这个问题或者是否有解决方法?

4

2 回答 2

2

如果您在 Android Studio 中使用 AsyncTask 来咨询 mysql 中的 HttpURLConnectionin 或其他数据库的 Gestor。有一个名为“String downloadUrl(String myurl)”的方法有一个变量“int len = 500;” 对于 inmputStream 中的读取数据,您可以将此变量更改为需要读取的数字字符。

 private String downloadUrl(String myurl) throws IOException {
    Log.i("URL",""+myurl);
    myurl = myurl.replace(" ","%20");
    InputStream is = null;
    // Only display the first 500 characters of the retrieved
    // web page content.
    <b>int len = 1500; </b>

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        Log.d("respuesta", "The response is: " + response);
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = readIt(is, len);
        return contentAsString;

        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
于 2016-09-20T15:35:23.633 回答
0

@Daniela 正确地发现了问题。我使用的代码与开发者 android 网站中提到的代码相同。我的修复如下:

    InputStreamReader reader = new InputStreamReader(stream);
    BufferedReader br = new BufferedReader(reader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line+"\n");
    }
    br.close();
    return sb.toString();
于 2017-01-22T07:30:44.087 回答