通过问题上传httpPost的base64图像服务器 [英] Problems uploading base64 images to server by httpPost
问题描述
我在哪里原型我送当地SQLite数据库的使用JSON和HttpPost远程MySQL数据库的全部内容的应用程序。
I am prototyping an application where I send the whole content of the local SQLite database to a remote MySQL database using JSON and HttpPost.
一切正常的文本数据。
现在我添加图像到党和我,虽然我可以为base64字符串,我送走了JSON添加图像。我的图像是800×600像素,在每一个尺寸或多或少500KB。
Everything works fine for text data. Now I added images to the party and I though I could add the images as base64 strings to the JSON I am sending away. My images are 800 x 600 pixels, more or less 500kb in size each.
如果我贴我的应用程序手动生成一个网页的JSON,它是好的,我得到我的图片和其他一切。
If I paste the JSON generated by my app manually to a web page, it is fine, I get my images and everything else.
使用应用程序上传脚本,我试着上传一个JSON字符串4张图片,但应用程序卡住我的进度对话框和logcat的推移遍地显示这样的:
Using the app uploading script, I tried to upload a JSON string with 4 images but the application gets stuck at my progress dialog and Logcat goes on over and over showing this:
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
和它永远继续下去。我敢打赌,图像太大这种方式发送,和/或我的地方内存泄漏。
and it goes on forever. I bet the images are too big to be sent this way, and/or I am leaking memory somewhere.
这将是一个更好的方法来使用JSON数据到服务器一起上传大尺寸的图像?否则,我怎么能避免内存泄漏?
What would be a better approach to upload big size images along with JSON data to a server? Otherwise, how can I avoid memory leaks?
在code是pretty标准...
The code is pretty standard...
从光标到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;
}
任何帮助将大大AP preciated
Any help will be greatly appreciated
推荐答案
您的解决方案只是加载所有要发送到内存中的数据。这是几乎没有可扩展性。考虑使用多部分实体来代替。
Your solution just loads all the data to be sent into memory. That's hardly scalable. Consider using a multipart entity instead.
File image1;
MultipartEntity entity = new MultipartEntity();
entity.addPart("json", new StringBody(serializedJson, Charset.forName("UTF-8"));
entity.addPart("image1", new FileBody(image1, "application/octet-stream");
这个例子显示,当我要发送的文件可在磁盘上的情况。通过创建自己的AbstractContentBody imlementation,你应该能够送东西位于你的数据库,而无需耗费太多的内存...
This example shows the case when i'm about to send a file available on the disk. By creating your own imlementation for AbstractContentBody, you should be able to send something located in your db, without consuming too much memory...
这篇关于通过问题上传httpPost的base64图像服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!