java - Android 改造 Base64 @Body

标签 java android square retrofit

大家好,我在 android 4.3 中有这段代码,我现在正在使用改造,但服务器向我抛出一条错误消息 “输入不是有效的 Base-64 字符串,因为它包含非 Base-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);

最佳答案

对于一般对象类型(包括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, "...", "...");

关于java - Android 改造 Base64 @Body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22787585/

相关文章:

java - java中stringbuilder中Fortify的拒绝服务问题

java - 处理共享资源上的信号量冲突的最佳实践是什么?

android - 错误 :Failed to resolve: com. squareup.sdk

c++ - 给定一个整数,将其表示为 s 个平方和

square - 如何修复 Lighthouse 返回的错误 : NOT_HTML. 提供的页面不是 square/weebly 网站的 HTML(用作 MIME 类型)错误?

java - 为什么有N个节点的AVL树会保持C<=N/2?

java - 功能通知在聊天应用程序 Firebase 中不起作用

java - SSL异常 : Could not find any key store entries to support the enabled cipher suites

android - 在 Eclipse 中使用 gson 与 Android 的问题

android - 是否有适合在已知环境中识别物体的图像处理库/AR 技术?