我正在对一个应用程序进行原型设计,在该应用程序中,我使用 JSON 和 HttpPost 将本地 SQLite 数据库的全部内容发送到远程 MySQL 数据库。
一切都适用于文本数据。现在我向聚会添加了图像,尽管我可以将图像作为 base64 字符串添加到我发送的 JSON 中。我的图像是 800 x 600 像素,每个大小或多或少 500kb。
如果我将我的应用程序手动生成的 JSON 粘贴到网页上,那很好,我得到了我的图像和其他所有内容。
使用应用程序上传脚本,我尝试上传一个带有 4 个图像的 JSON 字符串,但应用程序卡在我的进度对话框中,Logcat 一遍又一遍地显示:
11-15 14:32:27.809: I/dalvikvm-heap(15562): Grow heap (frag case) to 21.964MB for 2680048-byte allocation
11-15 14:32:27.840: D/dalvikvm(15562): GC_CONCURRENT freed 1744K, 30% free 20666K/29447K, paused 2ms+3ms
11-15 14:32:27.879: D/dalvikvm(15562): GC_FOR_ALLOC freed 4362K, 39% free 18049K/29447K, paused 16ms
11-15 14:32:27.918: D/dalvikvm(15562): GC_FOR_ALLOC freed 0K, 33% free 19794K/29447K, paused 22ms
11-15 14:32:27.918: I/dalvikvm-heap(15562): Grow heap (frag case) to 21.964MB for 2680276-byte allocation
11-15 14:32:27.958: D/dalvikvm(15562): GC_CONCURRENT freed 1744K, 30% free 20667K/29447K, paused 1ms+4ms
11-15 14:32:27.997: D/dalvikvm(15562): GC_FOR_ALLOC freed 4362K, 39% free 18049K/29447K, paused 17ms
11-15 14:32:28.028: D/dalvikvm(15562): GC_FOR_ALLOC freed 0K, 33% free 19795K/29447K, paused 17ms
它会永远持续下去。我敢打赌,图像太大而无法以这种方式发送,和/或我在某处泄漏了内存。
将大尺寸图像与 JSON 数据一起上传到服务器的更好方法是什么?否则,如何避免内存泄漏?
代码很标准...
从光标到 JSON:
private JSONObject get_images_data_JSON(Cursor c) {
JSONObject image_jo = new JSONObject();
//get unit identifier
long unit_identifier = c.getLong(c.getColumnIndex("_id"));
//set unit details
try {
image_jo.put("_id", unit_identifier);
image_jo.put("unit_id", c.getLong(c.getColumnIndex("unit_id")));
//encode blob in Base64 for json parsing
String encodedImage = Base64.encodeToString(c.getBlob(c.getColumnIndex("image")), Base64.DEFAULT);
image_jo.put("image", encodedImage);
image_jo.put("caption", c.getString(c.getColumnIndex("caption")));
} catch (JSONException e) {
e.printStackTrace();
}
return image_jo;
}//end get_images_data_JSON
和 POST 功能:
public String postData(JSONArray array) {
String responseMessage = "";
//set connection timeout values
HttpParams myParams = new BasicHttpParams();
//set timeout in milliseconds until a connection is established
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
//set timeout for waiting data
HttpConnectionParams.setSoTimeout(myParams, 10000);
HttpClient httpclient = new DefaultHttpClient(myParams);
//Get a string out of the JSONArray
String json = array.toString();
try {
HttpPost httppost = new HttpPost(URL);
httppost.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(json);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
responseMessage = inputStreamToString(inputStream);
//log out response from server
longInfo(responseMessage);
}
//show error if connection not working
catch (SocketTimeoutException e) {
e.printStackTrace();
responseMessage = "unreachable";
}
catch (ConnectTimeoutException e) {
e.printStackTrace();
responseMessage = "unreachable";
}
catch (Exception e) {
e.printStackTrace();
responseMessage = "unreachable";
}
return responseMessage;
}
任何帮助将不胜感激