发送JSON内的图像到Android的web服务
我想从我的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);
}
您应该使用一个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数组。
好的,谢谢。我无法访问服务器端。我知道的一切就是它能够根据预定义的JSON反序列化数据。我会测试Genson。 – psv 2014-10-09 11:36:07
服务器端将与您拥有相同的约束(关于最大字符串大小),所以服务器代码将不得不更新以支持您使用的格式。关于整数的使用,您仍然会有一个限制(超出内存限制),即整数的最大值,请参阅http://*.com/questions/816142/strings-maximum-length-in- java-calling-length-method和http://*.com/questions/12120002/is-there-any-limit-for-string-size-in-a-java-program。 – eugen 2014-10-09 13:59:37
当我使用邮递员测试服务器时,它接受我发送非常长的json(以及图像字段中的Base64中的非常长的字符串)。对我来说,服务器端对Image字符串的长度没有任何问题 – psv 2014-10-09 14:15:29
字符串“太短”如何包含base64? – 2014-10-09 10:03:17
我有这样的错误:https://community.oracle.com/thread/1524893?start=0&tstart=0 – psv 2014-10-09 10:07:37
@psv你是否设法解决你的问题?你最终使用了什么解决方案? – eugen 2014-10-10 23:27:45