我目前希望通过PHP捕获打印数据来从远程服务器传输数据,然后将其存储到设备的SQLite数据库中。
问题是,有时我需要从表中接收超过6.000行,将所有数据存储在设备的RAM内存(变量)中无效,所以我想知道是否有人请告诉我如何将传入的数据保存到设备的内存(sd,资源或资源)中,并在处理成SQLite数据库后将其删除。我目前用于接收数据的方法是:
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();
}
通过使用此方法,我收到此错误:
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)
对不起,我很感激任何帮助!
答案 0 :(得分:3)
查看Google的GSON Library,特别是JsonReader类。
您可以使用它来逐个元素地“拉出”JSON数据,而不是将整个文档读入对象结构(正如您所说的那样具有大的内存影响)。然后,您可以一次读取一行JSON数据并将其写入数据库。
JsonReader实际上是来自API级别11的Android的一部分。如果您针对早期版本的Android进行编译,则可以使用GSON stream jar。