大家好我在android 4.3中有这个代码,我现在正在使用改造,但服务器向我抛出了一条错误消息 “输入不是有效的Base-64字符串,因为它包含非基本64个字符,两个以上的填充字符或填充字符中的非法字符。”当我使用改造时
//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);
答案 0 :(得分:24)
对于常规对象类型(包含String
)Retrofit将使用其Converter
来序列化该值。在这种情况下,默认情况下使用Gson将主体序列化为JSON。
要上传您要使用的TypedInput
的Base64编码数据。这告诉Retrofit您将传递已经序列化的原始主体和相关的Content-Type
值。
@PUT("/PhotoService/{PROPERTYID}/{WATERMARK}")
String uploadPhoto(
@Body TypedInput photo,
@Path("PROPERTYID") String propertyId,
@Path("WATERMARK") String watermark);
我将在上面的例子中假设b
是byte[]
。在这里,我使用的是TypedInput
:TypedByteArray
TypedInput body = new TypedByteArray("application/x-www-form-urlencoded", b);
service.uploadPhoto(body, "...", "...");