【发布时间】:2012-02-16 10:17:48
【问题描述】:
我目前希望通过 PHP 捕获打印数据从远程服务器传输数据,然后将其存储到设备的 SQLite 数据库中。
问题是有时我需要从一个表中接收超过 6000 行,将所有数据存储在设备的 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)
抱歉发了这么长的帖子,任何帮助将不胜感激!
【问题讨论】:
标签: android json resources download inputstream