Reputation: 651
My android application use this code for to read and import 4700 records from mysql database on the web with JSONArray :
@Override
protected String doInBackground(Void... params) {
try{
httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.example.com/preleva_prodotti.php");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
//Log.e("log_tag", "Error in http connection "+ e.toString());
httpclient.getConnectionManager().shutdown();
return result = "no_server";
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
String k = reader.readLine();
while ((line = k) != null) {
sb.append(line + "\n");
}
is.close();
result=sb;
}catch(Exception e){
return result = "no_server";
}
//parse json data
try{
JSONObject json= new JSONObject(result);
JSONArray jArray = json.getJSONArray("array");
return is_full="ok_insert";
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
return is_full = "no_dati";
}
}
but at the line :
while ((line = k) != null) {
sb.append(line + "\n");
}
there is the error : Out of Memory. How can resolve this problem?
Upvotes: 1
Views: 448
Reputation: 101604
I'm not too familiar with Java, so bear with me, but this seems to be language-agnostic.
That said, the problem appears to be that k
is never updated. Once k
is assigned (k = reader.readLine()
) you're (indefinitely) re-assigning line
that same value (thus infinite loop, thus out of memory).
I think you're looking for:
String k = reader.readLine();
while ((line = k) != null) {
sb.append(line + "\n");
k = reader.readLine(); // or some facsimile
}
Note that I now re-assign k
(though I'm not sure the redundancy of having k
= line
; seems like a waste of a variable, imho)
Upvotes: 2