Parse.com Android 通过 Rest Api 上传图片 "invalid utf-8 string was provided"

Parse.com Android upload Image "invalid utf-8 string was provided" by RestApi

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

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

在 Parse 上,我制作了一个 Class "ImageProfile",其中包含一个类型文件字段,在我的客户端中,我有这个:

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

在我的Activity

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

您的 Content-Type 可能会导致您的错误响应,因为您正尝试发送一个包含 JPEG 图像作为 MimeType 的多部分请求。所以你 Content-Type 应该是 form/Multipart

检查您正在使用的 RestAdapter 是否在 application/json header

之前拦截

问题已解决

我创建了一个class“ImageProfile”并添加了一个“imageFile”属性的File类型,最后尝试在这个属性.

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

file type field in parse.com expects a json with the following structure.

{

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

然后我发现 File 类型只是指向 url https://api.parse.com/1/files/name.extension

保存的文件的指针

所以我们应该post到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

An important caveat, the Content-Type should be (image / jpeg or png) and not Multipart as expected.

Multipart form-data is not supported when uploading files

回复:

{
    "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"
}

Official Documentation