2014-10-09 64 views
0

我想從我的android設備發送一個json對象到服務器(在後)。發送JSON內的圖像到Android的web服務

在我的json對象中,我需要在Base64中添加一個圖像。我無法使用String來將圖像轉換爲Base64,因爲String太短而無法包含Base64編碼文件。 我必須使用BytesArray。

如何發送類似的東西到JSON的web服務?

{ 
"emergency":"gsjqsbl", 
"cod_valid":"O", 
"image":{ 
    "content":"/9j/4AAQSkZJRg ## MY VERY VERY VERY VERY VERY VERY VERY LONG IMAGE IN BASE64 ## BWNn+SV5H8KQADnn9ysrKA1EVX+lNDkSJ8ai8UADCLoAR3TWVlHT95AVvcfwCvD4Hq1joP5NX3Ciat7zyP4VlZW9bnl1sf//Z", 
    "content_type":"image/jpg" 
}, 
"indoor":"yes", 
"access_conditional":"text", 
"geo_shape":{ 
    "type":"Point", 
    "coordinates":[ 
    2.0202024, 
    45.799005 
    ] 
}, 
"lastupdate":"", 
"ref_fr_sdis91":"", 
"name":"TEST IN UPPERCASE WITH SPECIAL CHARACTERS ;/*-é@$~æ€", 
"geo_point_2d":"45.799005,2.0202024", 
"source":"soures", 
"objectid":"", 
"aed_location":"Spopopo" 
} 

我真的不能使用String。

感謝

編輯:什麼我還沒有完成:

//output stream 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

    //write text 
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); 
    BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); 
    bufferedWriter.write("json start { blabla: value"); 

    //Write image 
    AssetManager assetManager = context.getAssets(); 

    InputStream istr; 
    Bitmap bitmap = null; 
    try { 
    istr = assetManager.open("pictures/defib12.jpg"); 
    bitmap = BitmapFactory.decodeStream(istr); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos); 
    outputStream.write(Base64.encode(baos.toByteArray(), Base64.DEFAULT)); 

    //write text 
    OutputStreamWriter outputStreamWriter2 = new OutputStreamWriter(outputStream); 
    bufferedWriter = new BufferedWriter(outputStreamWriter2); 
    bufferedWriter.write("json end }"); 

    HttpResponse response = restClient.executePostRequest(pushUrl, outputStream); 

和:

public HttpResponse executePostRequest(String url, ByteArrayOutputStream outputStream) throws IOException { 
LogWrapper.debug(DmaRestClient.class, "New HttpPost request: " + url); 

DefaultHttpClient client = new DefaultHttpClient(); 
HttpPost request = new HttpPost(url); 

request.setEntity(new ByteArrayEntity(outputStream.toByteArray())); 
request.setHeader("Content-Type", "application/json"); 

return client.execute(request); 
} 
+0

字符串「太短」如何包含base64? – 2014-10-09 10:03:17

+1

我有這樣的錯誤:https://community.oracle.com/thread/1524893?start=0&tstart=0 – psv 2014-10-09 10:07:37

+0

@psv你是否設法解決你的問題?你最終使用了什麼解決方案? – eugen 2014-10-10 23:27:45

回答

0

您應該使用一個lib來做到這一點。檢查Genson,它提供了兩種ser/de二進制內容的機制:使用經典的base64編碼的字符串(但在你的情況下它看起來不起作用)或ser/de作爲整數的數組。請注意,您必須在服務器端上使用相同的機制 - 但它可能是另一個支持這種格式的庫。

與Genson,你會創建類來表示你的JSON和image.content值將是字節數組。爲了能夠在Genson此選項:

Genson genson = new GensonBuilder().useByteAsInt(true).create(); 
genson.serialize(yourObject, theOutputStream); 

如果你想通過做手工,而無需使用任何lib中你還可以(但這是一個不好的做法 ...)使用同樣的伎倆=>絲氨酸/將字節數組作爲int數組。

+0

好的,謝謝。我無法訪問服務器端。我知道的一切就是它能夠根據預定義的JSON反序列化數據。我會測試Genson。 – psv 2014-10-09 11:36:07

+0

服務器端將與您擁有相同的約束(關於最大字符串大小),所以服務器代碼將不得不更新以支持您使用的格式。關於整數的使用,您仍然會有一個限制(超出內存限制),即整數的最大值,請參閱http://stackoverflow.com/questions/816142/strings-maximum-length-in- java-calling-length-method和http://stackoverflow.com/questions/12120002/is-there-any-limit-for-string-size-in-a-java-program。 – eugen 2014-10-09 13:59:37

+0

當我使用郵遞員測試服務器時,它接受我發送非常長的json(以及圖像字段中的Base64中的非常長的字符串)。對我來說,服務器端對Image字符串的長度沒有任何問題 – psv 2014-10-09 14:15:29