0

I am getting the error when creating a JSONObject by passing a string.

Org.json.JSONException: Unterminated string at character

My JSON string is:

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

This seems to only happen on Linux as I developed in Windows and it worked fine. Also, the issue seems to stem from the string being too long. I cut the array by more than half and the issue went away.

What can I do to fix this issue or if there is a workaround?

vk239
  • 1,014
  • 1
  • 12
  • 30
drum
  • 5,416
  • 7
  • 57
  • 91
  • Where are you getting the string from? It's possible the string is being cut short _before_ it is passed to the JSON library – Andrew Williamson Feb 12 '16 at 16:47
  • Well `rec1` isn't valid JSON. Please provide a short but *complete* example that's demonstrating the problem - and explain how you're obtaining the string, which may well be causing the issue. – Jon Skeet Feb 12 '16 at 16:50
  • I'm reading it from a file using this 1-liner `String jsonString = new Scanner(new File(source)).useDelimiter("\\Z").next();` – drum Feb 12 '16 at 16:51
  • 1
    If you print the string before parsing, is it cut short? If not, you might want to test it with another JSON library, like Google's Gson. @JonSkeet How are you so omnipresent on Stack Overflow? – Andrew Williamson Feb 12 '16 at 17:00

2 Answers2

2

if you are using AsyncTask in Android studio for to make consult with HttpURLConnectionin in mysql or other Gestor of Data Base. There is a method called "String downloadUrl(String myurl) " there is a variable "int len = 500;" for read data in inmputStream, you change this variable for a number caracther that you need to read.

 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();
        }
    }
}
Daniela
  • 21
  • 4
0

@Daniela caught the problem correctly. I was using the same code as mentioned in developer android website. My fix below:

    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();
user1985
  • 56
  • 1
  • 4