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?
JSON
so multipart is not required – Ramesh sambu