Download update 2 binary in Android

I want to upload a binary file to a server in Android. I check the Api method by the postman:

enter image description here

And this is normal, as you can see, there is another option that you can upload files in the form of form data (key, value):

enter image description here

Each tutorial (like this one ) describes how to upload files like multipart/form-data:

 // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // MultipartBody.Part is used to send also the actual file name
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

I am looking a lot, but cannot find a way to download the file in binary using retrofit2.

There is only one problem in the updated repository, which asks how I can place the image binary in a modified beta 2.0? I use his solution:

API Service:

@POST("trip/{tripId}/media/photos")
Call<MediaPost> postEventPhoto(
    @Path("eventId") int tripId,
    @Header("Authorization") String accessToken,
    @Query("direction") String direction,
    @Body RequestBody photo);

Subscriber:

InputStream in = new FileInputStream(new File(media.getPath()));
byte[] buf;
buf = new byte[in.available()];
while (in.read(buf) != -1);
RequestBody requestBody = RequestBody
    .create(MediaType.parse("application/octet-stream"), buf);
Call<MediaPost> mediaPostCall = tripApiService.postTripPhoto(
    tripId,
    ((GlobalInfo) getApplicationContext()).getApiAccessToken(),
    direction,
    requestBody);

But I got this error:

java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. 

What is wrong with my code? what should I do?

+7
4

, API @Multipart! , !

+4

//Api Interface

@Part MultipartBody.Part body


//Call in activity
     file = FileUtils.getFile(this, uri);
                            reqFile = RequestBody.create(MediaType.parse("image/*"), file);
                            body = MultipartBody.Part.createFormData("uploaded_file", "Img_" + "_" + rightNow.getTimeInMillis() + ".jpg", reqFile);
0

, , , .

, byte[] RequestBody. :

interface RetrofitService {
    @POST("api/v1/upload_file")
    Call<Void> uploadBinaryFile(@Body RequestBody body);
}

:

public void uploadBinaryFile(File fileToUpload) {
    retrofitService
        .uploadBinaryFile(RequestBody.create(MediaType.parse("application/octet"), 
            Files.readAllBytes(fileToUpload));
}

, OP, .

0
source

just MediaType == Null my code is:

  private fun put(uploadUrl : String , localPath : String) {
        val file = File(localPath)
        val byteArray = file2Byte(localPath)!!
        val responseBody = RequestBody.create(null , byteArray)
        val call = HttpFactory.providerUp.up(uploadUrl , responseBody)

        call.enqueue(object : Callback<ResponseBody> {
            override fun onFailure(call : retrofit2.Call<ResponseBody>? , t : Throwable?) {
                LogUtil.toast("Failure")
            }

            override fun onResponse(call : retrofit2.Call<ResponseBody>? , response : retrofit.Response<ResponseBody>?) {
                if (response!!.code() == 200) {
                    LogUtil.toast("YES")
                } else {
                    LogUtil.toast("NO")
                }
            }
        })
    }
@PUT
    fun up2(@Url url : String ,@Body requestBody : RequestBody ) : Call<ResponseBody>
0
source

Source: https://habr.com/ru/post/1658670/


All Articles