1

我正在对一个应用程序进行原型设计,在该应用程序中,我使用 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;

}

任何帮助将不胜感激

4

2 回答 2

1

您的解决方案只是将所有要发送到内存的数据加载。这很难扩展。考虑改用多部分实体。

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 创建自己的实现,您应该能够发送位于数据库中的内容,而不会消耗太多内存......

于 2012-11-15T15:32:44.353 回答
1

对于任何来到这里的人,我按照 Vajk 的建议解决了所有问题,这是我的新 POST 功能:

public String postData(Cursor images, JSONArray json_array) {

    int counter = 0;
    String responseMessage = "";

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(URL);

        //convert JSON array to String
        String json_encoded_string = json_array.toString();

        //MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        MultipartEntity entity = new MultipartEntity();

        //add json data
        entity.addPart("json", new StringBody(json_encoded_string));

        //get all images plus data and add them to the Multipart Entity

        for (images_cursor.moveToFirst(); !images_cursor.isAfterLast(); images_cursor.moveToNext()) {

            counter++;

            //Get image as byte array
            byte[] image_ba = images_cursor.getBlob(images_cursor.getColumnIndex("image"));
            long image_unit_id = images_cursor.getLong(images_cursor.getColumnIndex("unit_id"));
            String image_caption = images_cursor.getString(images_cursor.getColumnIndex("caption"));

            //add image to multipart
            entity.addPart("image" + counter, new ByteArrayBody(image_ba, "image" + counter + ".jpg"));

            //add unit _id to multipart
            entity.addPart("image_unit_id" + counter, new StringBody(String.valueOf(image_unit_id)));

            //add caption to multipart
            entity.addPart("image_caption" + counter, new StringBody(String.valueOf(image_caption)));

        }

        httppost.setEntity(entity);

        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;
}

有人可能会觉得这很有用;)

于 2012-11-19T13:59:28.887 回答