1
votes

I want to rewrite uploading file to server using Retrofit.

The server api request for body is

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

Before uploading also need to compress the image and also encode the content. Our current implementation is:

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();
        ...
    }

I want to change the above code to Retrofit upload. After studying Retrofit Upload Example which uses OkHttp’s RequestBody or MultipartBody.Part classes. But i have no idea how to convert the above code.

Any suggestion?

1
your request is JSON so multipart is not requiredRamesh sambu

1 Answers

1
votes

1.Create interface with requests and add method

public interface PostRequests {

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

     }

2.Create pojo for request body

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.Init

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.Execute

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

You upload picture in String fromat(Base64), so you can put this String into pojo object.