JSON と HttpPost を使用して、ローカルの SQLite データベースのコンテンツ全体をリモートの MySQL データベースに送信するアプリケーションのプロトタイプを作成しています。
テキストデータの場合はすべて正常に機能します。今、パーティーに画像を追加しましたが、画像を base64 文字列として、送信している JSON に追加できました。私の画像は 800 x 600 ピクセルで、サイズはそれぞれ 500kb 程度です。
アプリによって手動で生成された JSON を Web ページに貼り付けても問題ありません。画像とその他すべてを取得できます。
アプリのアップロード スクリプトを使用して、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;
}
どんな助けでも大歓迎です