Im currently wishing to transfer data from a remote server by capturing printed data via PHP, which is stored into the device’s SQLite database afterwards.
The problem is that sometimes I need to receive over 6.000 rows from a table, it isn’t effective storing all that data in the device’s RAM memory (variables), so I was wondering if someone please could tell me how to save the incoming data into the device’s memory (sd, assets or resources) and delete it after being processed into the SQLite database. The method I’m currently using to receive the data is this:
public JSONArray requestTable(List<NameValuePair> nameValuePairs) throws ClientProtocolException, IOException, JSONException, UnknownHostException, ConnectTimeoutException
{
//NameValuePairs contains the Query so it can be catched by php's $_POST
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(http://something.php);
HttpParams httpparams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparams, msTimeout);
JSONArray tableData = null;
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse httpresponse = httpclient.execute(httppost);
Log.i("How are you?", httpresponse.getStatusLine().toString());
HttpEntity entity = httpresponse.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
if(result.charAt(0) == '<')
Log.e("php error", result);
else
tableData = new JSONArray(result);
instream.close();
httpresponse = null;
return tableData;
}
else
return null;
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 16 * 1024);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
By using this method I get this error:
01-22 17:43:54.098: INFO/dalvikvm-heap(380): Grow heap (frag case) to 18.874MB for 2834992-byte allocation
01-22 17:44:06.368: INFO/dalvikvm-heap(380): Forcing collection of SoftReferences for 4252484-byte allocation
01-22 17:44:08.490: ERROR/dalvikvm-heap(380): Out of memory on a 4252484-byte allocation.
01-22 17:44:08.490: INFO/dalvikvm(380): "Thread-8" prio=5 tid=7 RUNNABLE
01-22 17:44:08.490: INFO/dalvikvm(380): | group="main" sCount=0 dsCount=0 s=N obj=0x44f182d8 self=0x118bf8
01-22 17:44:08.490: INFO/dalvikvm(380): | sysTid=389 nice=0 sched=0/0 cgrp=default handle=2525744
01-22 17:44:08.498: INFO/dalvikvm(380): | schedstat=( 328938307540 280414788056 42400 )
01-22 17:44:08.498: INFO/dalvikvm(380): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~97)
01-22 17:44:08.498: INFO/dalvikvm(380): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:136)
01-22 17:44:08.498: INFO/dalvikvm(380): at java.lang.StringBuilder.append(StringBuilder.java:272)
01-22 17:44:08.498: INFO/dalvikvm(380): at java.io.BufferedReader.readLine(BufferedReader.java:452)
01-22 17:44:08.498: INFO/dalvikvm(380): at srdroid.cata.Server.convertStreamToString(Server.java:628)
01-22 17:44:08.498: INFO/dalvikvm(380): at srdroid.cata.Server.requestTable(Server.java:226)
01-22 17:44:08.498: INFO/dalvikvm(380): at srdroid.cata.Server$2.run(Server.java:418)
01-22 17:44:08.498: INFO/dalvikvm(380): at java.lang.Thread.run(Thread.java:1096)
Sorry for the long post, any help will be greatly appreciated!
Look at Google’s GSON Library, in particular the JsonReader class.
You can use this to “pull” JSON data out element by element rather than reading the entire document into an object structure (which as you say has a large memory impact). You can then read one row of JSON data at a time and write it to the DB.
JsonReader is actually part of Android from API level 11. If you’re compiling against an earlier version of Android you can use the GSON stream jar.