Android Retrofit Base64 @Body [英] Android Retrofit Base64 @Body
问题描述
大家好我在android 4.3中有这个代码,我现在正在使用改造,但是服务器向我抛出了一条错误消息
输入不是有效的Base-64字符串,因为它包含一个非基本的64字符,填充字符中有两个以上的填充字符或非法字符。当我使用改装时
Hi all i have this code in android 4.3 and i am using retrofit just now but server thrown me an error message "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters." When i am using retrofit
//Normal HttpClient
//Base64 String
photo = new String(b);
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPut httpPut = new HttpPut("http://beta2.irealtor.api.iproperty.com.my.ipga.local/PhotoService/"
+ mPropertyId + "/testWatermark"
);
httpPut.setHeader("content-type", "application/x-www-form-urlencoded");
httpPut.setHeader("Authorization","WFdSeW8vTJ1Z3oQlBJMk53VGpaekZRY2pCd1pYSlVXU090");
httpPut.setHeader("Accept", "application/json");
httpPut.setEntity(new StringEntity(photo, "utf-8"));
HttpResponse response = httpClient.execute(httpPut);
//With retrofit
@Headers({
"content-type:application/x-www-form-urlencoded"
})
@PUT("/PhotoService/{PROPERTYID}/{WATERMARK}") String uploadPhoto(
@Body String photo,
@Path("PROPERTYID") String propertyId,
@Path("WATERMARK") String watermark);
推荐答案
对于一般对象类型(字符串
包括)Retrofit将使用其 Converter
来序列化该值。在这种情况下,默认情况下使用Gson将主体序列化为JSON。
For general object types (String
included) Retrofit is going to use its Converter
to serialize the value. In this case, Gson is used by default to serialize the body as JSON.
为了上传您想要使用的Base64编码数据 TypedInput
。这告诉Retrofit您将传递已经序列化的原始主体和关联的 Content-Type
值。
In order to upload Base64-encoded data you want to use TypedInput
. This tells Retrofit that you will pass it the raw body which is already serialized and an associated Content-Type
value.
@PUT("/PhotoService/{PROPERTYID}/{WATERMARK}")
String uploadPhoto(
@Body TypedInput photo,
@Path("PROPERTYID") String propertyId,
@Path("WATERMARK") String watermark);
我将假设 b
在上面的示例中是 byte []
。这里我使用 TypedInput
的现有实现: TypedByteArray
I'm going to assume that b
is a byte[]
in your above example. Here I'm using an existing implementation of TypedInput
: TypedByteArray
TypedInput body = new TypedByteArray("application/x-www-form-urlencoded", b);
service.uploadPhoto(body, "...", "...");
这篇关于Android Retrofit Base64 @Body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!