我在将图像发布到我的 wcf 休息服务时遇到问题。我发布了一些参数,其中一个是 base64 utf-8 编码字符串(图像)。
我的问题是每次我发布我都会收到“错误请求”。这是代码
public String PostImage(Drawable img) throws Exception
{
HttpPost httpost = new HttpPost("http://10.0.2.2:1374/uploaditem");
JSONStringer json = JSONStringer()
.object()
.key("ipm")
.object()
.key("name").value("test")
.key("description").value("asfa")
.key("categoryid").value(1)
.key("data").value(ConvertImgToBase64Str(img))
.key("imagetype").value(2)
.key("tags").value("test;test")
.endObject()
.endObject();
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httpost.setEntity(entity);
return ExcecutePostRequest(httpclient,httpost);
}
//Method to convert the image to base64encoded string
private String ConvertImgToBase64Str(Drawable img) {
Bitmap bitmap = ((BitmapDrawable)img).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
return Base64.encodeToString(bitmapdata, Base64.URL_SAFE);
}
它与编码字符串有关,但是什么?