【问题标题】:Upload file to server using Retrofit which content is wrapped in JSON使用 Retrofit 将文件上传到服务器,其中内容包含在 JSON 中
【发布时间】:2018-06-07 04:06:04
【问题描述】:

我想使用 Retrofit 重写上传文件到服务器。

body 的服务器 api 请求是

{“file_name”: “my_pic”, “content_base64”: “MKMD….”}

在上传之前还需要压缩图片并对内容进行编码。我们当前的实现是:

Bitmap bmp = BitmapFactory.decodeFile(localPath);
ByteArrayOutputStream outputStream= new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
byte[] byteArrayImage = outputStream.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

JSONObject jObject = new JSONObject();

    try {
        jObject.put("file_name", "test.jpg");
        jObject.put("content_base64", encodedImage);
        String jObjectString = jObject.toString();
        URL url = new URL(serverPath);
        ...
        connection.setRequestMethod("PUT");
        connection.setUseCaches(false);
        OutputStreamWriter wr = new 
        OutputStreamWriter(connection.getOutputStream());
        wr.write(jObjectString);
        wr.close();
        ...
    }

我想把上面的代码改成 Retrofit 上传。在学习了使用 OkHttp 的 RequestBody 或 MultipartBody.Part 类的Retrofit Upload Example 之后。但我不知道如何转换上面的代码。

有什么建议吗?

【问题讨论】:

  • 你的请求是JSON 所以不需要多部分

标签: android retrofit2


【解决方案1】:

1.创建请求接口并添加方法

public interface PostRequests {

      @PUT("your_url")
      Call<YourResponse> upload(@Body YourRequest yourRequest);

     }

2.为请求正文创建pojo

public class YourRequest {

    @SerializedName("file_name")
    @Expose
    private String fileName;

    @SerializedName("content_base64")
    @Expose
    private String picture;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }
}

3.初始化

public class Api {

private PostRequests mPostRequests;

public Api() {
mPostRequests = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl("your_base_url")
        .build()
        .create(PostRequests.class);
 }

public Call<YourResponse> upload(String localPath) {
     Bitmap bmp = null;
     try {
         bmp = BitmapFactory.decodeFile(localPath);
         ByteArrayOutputStream outputStream = new        ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
    byte[] byteArrayImage = outputStream.toByteArray();
    String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
    YourRequest yourRequest = new YourRequest();
    yourRequest.setFileName("test.jpg");
    yourRequest.setPicture(encodedImage);
    return mPostRequests.upload(yourRequest);
}finally {
    if(bmp!=null)
        bmp.recycle();
     }
 }

}

4.执行

public void uploadMethod()
{
Api api=new Api();
api.upload("path").execute().body();// it is sync operation you can use eneque() for async
}

你上传的是String fromat(Base64)中的图片,所以你可以把这个String放到pojo对象中。

【讨论】:

  • 非常感谢。我得到了它。需要将其视为服务器的更新对象而不是文件上传。
  • 我还有一个问题。如果压缩后文件太大,使用图片内容编码字符串,会不会有问题?
  • @rodent_la 这取决于服务器设置。
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 2015-03-01
  • 2018-12-06
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多