Sha*_*hye 4 java android square retrofit
嗨所有我在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);
Jak*_*ton 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);
我将假设这b是byte[]上面的例子.这里我使用的是现有的实现TypedInput:TypedByteArray
TypedInput body = new TypedByteArray("application/x-www-form-urlencoded", b);
service.uploadPhoto(body, "...", "...");
| 归档时间: | 
 | 
| 查看次数: | 7285 次 | 
| 最近记录: |