Q:

Android - large data json download

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.
Some days ago everything was working fine, but the number of registers in this table grew up fastly and then the problem rises. 
This problem is not happening on iOS or on Android emulator, just on real Android device.
The code is stopping the download suddenly and throwing the error
org.json.JSONException: Unterminated string at character <cccc> of <json data>

That is, the code is not downloading the entire JSON data. Besides, the download stops in a different point always I run the code.
Someone know why that is happening?
This is the function which tries to recover the data from web service:
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")+"&";
        }

        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;
}

A:

Firstly, do not keep an image in a table. Keep it in your server space and keep the URL to the image in your table. When you need to load the image, load it with the URL kept in the table.
Secondly, JSON is a light weight data interchange format. You should limit the number of items it carries. Its advisable not to let it carry up to a hundred rows in your table(when converting to JSON). If you have to, then load a little at first, then load another little, till it gets to the end. That's the idea behind infinite scrolling. 
That way, it loads your data efficiently. 

