Org.json.JSONException:字符处未终止的字符串

时间:2016-02-12 16:39:26

标签: java org.json jsonexception

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

git

我的JSON字符串是:

Org.json.JSONException: Unterminated string at character

这似乎只发生在Linux上,因为我在Windows中开发并且运行正常。此外,问题似乎源于字符串太长。我将阵列切割了一半以上,问题就消失了。

我该怎么做才能解决此问题或是否有解决方法?

2 个答案:

答案 0 :(得分:1)

如果你在Android studio中使用AsyncTask来在mysql或其他Gestor of Data Base中查询HttpURLConnectionin。有一种名为" 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();
        }
    }
}

答案 1 :(得分: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();