Parse.com 安卓上传图片 "invalid utf-8 string was provided" 由 RestApi



我正在尝试使用改装库将我的图像文件发送到 Parse.com,但出现以下错误:

{"code":107,"error":"invalid utf-8 string was provided"}

在 Parse 上,我创建了一个带有 File 字段的类"ImageProfile",在我的客户端中我有这个:

 @Multipart
 @POST("/classes/ImageProfile")
 void updateUser(@Part("imageFile") TypedFile photo, Callback<Images> callback);

在我的活动中

File file = new File(mCurrentPhotoPath);
String mimeType = "image/jpeg";
TypedFile fileToSend = new TypedFile(mimeType, file);
 service.updateUser(fileToSend, new Callback<Images>() {
            @Override
            public void success(Images images, Response response) {
                Log.d("tag", "Image Name " + images.getName());
            }
            @Override
            public void failure(RetrofitError error) {
                Log.d("tag", "Error: " + error.toString());
            }
});

日志:

03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ <--- HTTP 400  https://api.parse.com/1/classes/ImageProfile (16254ms)
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Access-Control-Allow-Methods: *
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Access-Control-Allow-Origin: *
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Content-Type: application/json; charset=utf-8
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Date: Sun, 15 Mar 2015 17:55:29 GMT
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Server: nginx/1.6.0
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ X-Parse-Platform: G1
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ X-Runtime: 0.001086
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Content-Length: 57
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ Connection: keep-alive
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ OkHttp-Selected-Protocol: http/1.1
03-15 13:54:46.921  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ OkHttp-Sent-Millis: 1426438486354
03-15 13:54:46.926  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ OkHttp-Received-Millis: 1426438486924
03-15 13:54:46.931  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ {"code":107,"error":"invalid utf-8 string was provided"}
03-15 13:54:46.931  21520-21855/br.com.andersonacs.sample.retrofit D/Retrofit﹕ <--- END HTTP (57-byte body)
03-15 13:54:46.931  21520-21520/br.com.andersonacs.sample.retrofit D/andre﹕ Error: retrofit.RetrofitError: 400 Bad Request

问题已解决

我创建了一个类"ImageProfile"并添加了文件类型的"imageFile"属性,最后尝试在此属性中保存图像。

这导致了错误{"code": 107, "error": "invalid utf-8 string was provided"}因为字段"imageFile"需要一个JSON。

parse.com 中的文件类型字段需要具有以下内容的 JSON 结构。

{
 "field name": {
      "name": "...image_name.png",
      "__type": "File"
    }
}

然后我发现文件类型只是指向由url保存的文件的指针 https://api.parse.com/1/files/name.extension

所以我们应该发帖 https://api.parse.com/1/files/name.extension

例如:

curl -X POST 
  -H "X-Parse-Application-Id: {APP ID}" 
  -H "X-Parse-REST-API-Key: {REST KEY}" 
  -H "Content-Type: image/jpeg" 
  --data-binary '@mmp.png.jpg' 
  https://api.parse.com/1/files/mmp.png

一个重要的警告,内容类型应该是(图像/jpeg或png) 而不是预期的多部分。

上传时不支持多部分表单数据 文件

回复 :

{
    "name": "tfss-5724175f-74b6-4a03-8443-2ebbaef90ba6-mmp.png",
    "url": "http://files.parsetfss.com/71e6836f-6005-4e8b-a4f7-99154ccf2b33/tfss-5724175f-74b6-4a03-8443-2ebbaef90ba6-mmp.png"
}

现在我们可以将此图像与ImageProfile对象相关联,如下所示:

curl -X PUT 
  -H "X-Parse-Application-Id: APP_ID" 
  -H "X-Parse-REST-API-Key: KEY" 
  -H "Content-Type: application/json" 
  -d '{ "imageFile": { "name": "tfss-5724175f-74b6-4a03-8443-2ebbaef90ba6-mmp.png", "__type": "File" } }' 
  https://api.parse.com/1/classes/ImageProfile/OBJECT_ID

响应:

{
    "imageFile":{
    "__type": "File",
    "name": "tfss-5724175f-74b6-4a03-8443-2ebbaef90ba6-mmp.png",
    "url": "http://files.parsetfss.com/71e6836f-6005-4e8b-a4f7-99154ccf2b33/tfss-5724175f-74b6-4a03-8443-2ebbaef90ba6-mmp.png"
    },
    "updatedAt": "2015-03-17T12:58:23.183Z"
}

官方文档

您的内容类型可能会导致错误响应,因为您尝试发送包含 JPEG 图像作为 MimeType 的多部分请求。所以你的内容类型应该是形式/多部分

检查您使用的 RestAdapter 是否在应用程序/json 标头之前没有拦截

相关内容

最新更新