4

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);
4

1 回答 1

24

对于一般对象类型(String包括),Retrofit 将使用它Converter来序列化值。在这种情况下,默认使用 Gson 将正文序列化为 JSON。

为了上传您想要使用的 Base64 编码数据TypedInput。这告诉 Retrofit 你将把已经序列化的原始主体和关联Content-Type值传递给它。

@PUT("/PhotoService/{PROPERTYID}/{WATERMARK}")
String uploadPhoto(
    @Body TypedInput photo,
    @Path("PROPERTYID") String propertyId,
    @Path("WATERMARK") String watermark);

我将假设这bbyte[]您上面示例中的 a 。在这里,我使用的是现有的实现TypedInputTypedByteArray

TypedInput body = new TypedByteArray("application/x-www-form-urlencoded", b);
service.uploadPhoto(body, "...", "...");
于 2014-04-01T22:54:19.307 回答