title: Android - large data json download
body: <p>I'm facing a problem in my app Android that consists in a failure to recover large json data from web service. The JSON data is large because contains images which are recovered from a table in my database.</p>
  
  <p>Some days ago everything was working fine, but the number of registers in this table grew up fastly and then the problem rises. </p>
  
  <p>This problem is not happening on iOS or on Android emulator, just on real Android device.</p>
  
  <p>The code is stopping the download suddenly and throwing the error</p>
  
  <pre><code>org.json.JSONException: Unterminated string at character &lt;cccc&gt; of &lt;json data&gt;
  </code></pre>
  
  <p>That is, the code is not downloading the entire JSON data. Besides, the download stops in a different point always I run the code.</p>
  
  <p>Someone know why that is happening?</p>
  
  <p>This is the function which tries to recover the data from web service:</p>
  
  <pre><code>protected static JSONObject executeJSONQuery(ContentValues values, 
            WebServiceResolverCode code, Context context)
              throws ContratoInativoException, JSONException {
  
      URL url;
      HttpURLConnection conn;
      JSONObject jArray = null;
  
      // build the string to store the response text from the server
      String response = "";
  
      try {
          url = new URL(WebServiceResolver.getWebServiceSufix(code, context));
  
          String param = "";
          for (String key : values.keySet()) {
              Object value = values.get(key);
              param += key+"="+ URLEncoder.encode(value!=null?values.getAsString(key):"", "UTF-8")+"&amp;";
          }
  
          conn = (HttpURLConnection) url.openConnection();
          conn.setDoOutput(true);
          conn.setRequestMethod("POST");
  
          conn.setFixedLengthStreamingMode(param.getBytes().length);
          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  
          // send the POST out
          OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
          out.write(param);
          out.flush();
          out.close();
  
          // start listening to the stream
          Scanner inStream = new Scanner(conn.getInputStream());
          while (inStream.hasNextLine()){
              response += (inStream.nextLine());
          }
          inStream.close();
          // process the stream and store it in StringBuilder
          jArray = new JSONObject(response);
  
      } catch (IOException ex) {
          ex.printStackTrace();
          Log.d("ERROR JSON STRING", response);
      }
      return jArray;
  }
  </code></pre>