【问题标题】:Android retrofit problem sending images in HTTP requestAndroid改造问题在HTTP请求中发送图像
【发布时间】:2020-08-30 16:15:54
【问题描述】:

我正在尝试将来自相机的图像作为 base64 字符串发送到我的 Spring Boot 后端。我正在使用改造。图像位于对象中。在调试图像时加载并且它们不为空。每个图像的额外信息大小为 +-2 MB。当我试图在我的请求中将它们发送到我的后端时,它们突然变为空。我不知道我做错了什么,而且我发现它们在我通过 HTTP 发送的对象中不为空,这很奇怪。有什么建议吗?

服务:

interface ImageApiService {

    @POST("imageObject")
    fun postNewAccidentStatement(@Body imageObject: ImageObject) : Deferred<ImageObject>
}

ImageObject 类:

@Entity(tableName = "imageObject_table")
@SuppressWarnings(RoomWarnings.PRIMARY_KEY_FROM_EMBEDDED_IS_DROPPED)
@TypeConverters(DateConverter::class)
data class ImageObject(

 @TypeConverters(ImageConverter::class)
    @Json(name = "images")
    @Expose
    var imagesAccident: List<Image>? = null
)

图像类:

@Entity(tableName = "image_table")
data class Image (
    @PrimaryKey(autoGenerate = true)
    var id: Int? = 0,
    var base64Img: String? = ""
)

【问题讨论】:

  • 你能添加更多的代码和细节吗?

标签: android android-studio kotlin retrofit


【解决方案1】:
 JSONObject obj= new JSONObject();
 obj.put("file", "data:image/png;base64,"+imgbase64); 
 //add other object field

 //here call api retrofit and send obj.toString()

【讨论】:

  • 我想通过我的请求将它作为 base64 字符串而不是文件发送
  • 请查看我的更新问题,包括我的课程,以便您了解我想如何通过 HTTP 将其作为 List 与属性 base64 String 发送
【解决方案2】:

一种可能的方法是使用哈希映射。所以你有一个你已经编码的base64图像。

HashMap<String, String> map = new HashMap<>();
        map.put("image", image);

在您的改造服务中这样做:

@FormUrlEncoded
@POST("image")
Call<ImageClass> getImage(@FieldMap HashMap<String, String> image);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多