1
我目前希望通過PHP捕獲打印數據來傳輸遠程服務器中的數據,之後將PHP存儲到設備的SQLite數據庫中。Android使用JSON從服務器下載大量數據
的問題是,有時我需要從表收到超過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)
對不起,長的帖子,任何幫助將不勝感激!
我目前正在使用API級別4的兼容性問題,感謝您的意見!我會給它一個鏡頭 –