2017-09-01 39 views
0

我有自定义对象的ArrayList,每个约10场,我已经成功上传到我们的服务器。问题是,该领域中的一个是含有一个Base64编码的字符串,我从文件中,改造GSON创建人似乎不喜欢转换的字符串。这个问题可以通过只发送所有字段,而图像来解决,然后以后使用FTP上传的所有图片,但它会容易得多,如果我可以把图像中的物体以某种方式。对象名单上传到mysql使用Android的改造

问题:如何使用Retrofit将自定义对象内的字段作为字段发送到URL?

+0

是什么,当你试图创建具有GSON对象的问题? – barotia

回答

1
@FormUrlEncoded 
@POST("/UploadImages") 
Call<ResponseBody> postImages(@Body ArrayListImage img); 

//POJO CLASS 

public class ArrayListImage { 
    @SerializedName("image") 
    @Expose 
    private ArrayList<String> image; 
    public ArrayListImage(ArrayList<String> image) { 
     this.image=image; 
    } 
} 
1

下面是RequestBody码,我用用Retrofit上传文件:

RequestBody lRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file); 
MultipartBody.Part lFile = MultipartBody.Part.createFormData("file", file.getName(), lRequestBody); 
MultipartBody.Part title = MultipartBody.Part.createFormData("title", file.getName()); 
MultipartBody.Part lFilenamebase64 = MultipartBody.Part.createFormData("filenamebase64", base64EncodedFileName); 

要编码的文件名:

String base64EncodedFileName = Base64.encodeToString(file.getName().getBytes(Charsets.UTF_8), Base64.URL_SAFE | Base64.NO_WRAP); 

我定义的API,如:

@Multipart 
@POST("/upload") 
Observable<Response<ResponseBody>> uploadFile(@Part MultipartBody.Part file, @Part MultipartBody.Part title, @Part MultipartBody.Part base64EncodedFileName); 

我希望它可以帮助你。