Halo伙计们,我想下载一个来自sql php的长字符串, 并在getHttpRespose()方法中存储为“result”,
并使用此“结果”更改为数据行并添加到android数据库中, 但是一些低模型的android手机导致[outofmemoryerror bytearrayoutputstream]错误, 那么如何修改我的代码来解决这个问题?
谢谢你的帮助!private static String getHttpResponse() {
String result = "";
HttpGet request = new HttpGet(API_URL);
HttpClient client = new DefaultHttpClient();
InputStream inputStream = null;
try {
HttpResponse response = client.execute(request);
Log.i("HTTP STATUS", ":" + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
Log.i("HTTP STATUS 200", "CONNECTION SUCCESS");
result = EntityUtils.toString(response.getEntity());
jsonString = result;
}
} catch (ClientProtocolException e) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
e.printStackTrace();
} catch (IOException e) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
e.printStackTrace();
} catch (Exception e) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
e.printStackTrace();
}
return result;
}
public static void addRecord(){
createTable();
InsertHelper ih = new InsertHelper(db, TABLE_CONTACTS);
db.setLockingEnabled(false);
db.beginTransaction();
try {
JSONObject jobject = new JSONObject(jsonString);
JSONArray jarray = jobject.getJSONArray("result");
int tempCategory = 0;
for (int i = 0; i < jarray.length(); i++) {
JSONObject oneObject = jarray.getJSONObject(i);
ih.prepareForInsert();
// Add the data for each column
ih.bind(1, checkNull(oneObject, "storeID"));
ih.bind(2, checkNull(oneObject, "tradeID"));
ih.bind(3, checkNull(oneObject, "hasBranch"));
ih.bind(4, checkNull(oneObject, "districtID"));
ih.bind(5, checkNull(oneObject, "categoryID"));
ih.bind(6, checkNull(oneObject, "name_en"));
ih.bind(7, checkNull(oneObject, "name_tc"));
ih.bind(8, checkNull(oneObject, "name_sc"));
ih.bind(9, checkNull(oneObject, "addr_en"));
ih.bind(10, checkNull(oneObject, "addr_tc"));
ih.bind(11, checkNull(oneObject, "addr_sc"));
ih.bind(12, checkNull(oneObject, "short_addr"));
ih.bind(13, checkNull(oneObject, "tel"));
ih.bind(14, checkNull(oneObject, "lat"));
ih.bind(15, checkNull(oneObject, "lng"));
ih.bind(16, checkNull(oneObject, "logo_url"));
ih.bind(17, checkNull(oneObject, "img_url"));
ih.bind(18, checkNull(oneObject, "coupon_url"));
ih.bind(19, checkNull(oneObject, "sort_type"));
ih.bind(20, checkNull(oneObject, "sort_order"));
ih.bind(21, checkNull(oneObject, "sort_type_tc"));
ih.bind(22, checkNull(oneObject, "sort_order_tc"));
ih.bind(23, checkNull(oneObject, "sort_type_sc"));
ih.bind(24, checkNull(oneObject, "sort_order_sc"));
ih.execute();
isSuccess=true;
isFirst=true;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
e.printStackTrace();
Log.i("errorJSON", "errorJSON");
} finally {
ih.close(); // See comment below from Stefan Anca
}
db.setTransactionSuccessful();
db.endTransaction();
done = true;
}
11-03 19:24:23.193: ERROR/AndroidRuntime(28258): FATAL EXCEPTION: main
11-03 19:24:23.193: ERROR/AndroidRuntime(28258): java.lang.OutOfMemoryError
11-03 19:24:23.193: ERROR/AndroidRuntime(28258): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)
11-03 19:24:23.193: ERROR/AndroidRuntime(28258): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
答案 0 :(得分:0)
您可以在AndroidManifest.xml中使用android:largeHeap =“true”,以便从Android的虚拟机中请求更多内存。