Picture arrives null on the server

Asked

Viewed 79 times

1

I am sending data using the retrofit, I observe all the request until the end and I get the code 200 more on the server side the image arrives null, as I am sending the image and text arrives correctly, no problems however the image does not, already changed the code, searched looked tutorials, initially I was sending a list of images, changed I’m sending only one plus this is not the problem my server is in Java

The whole process starts by getting image from the camera, the Activity data is recorded in the Sqlite database and later by the action of the user it calls a service that sends the record to the server.

Here is the Webserviceapi.java interface

@Multipart
@POST("fw_file.rule")
public Call<WsViagemResponse> sendViagem3(
        @Part("metodo") RequestBody metodo,
        @Part("parametros") RequestBody parametro,
        @Part MultipartBody.Part images
);

here the code in the class responsible for transmitting the data

MultipartBody.Part file1 = prepareFilePart("arquivo1.jpg", v.getFoto());
// Retrofit
WebServiceApi apiService = ApiClient
            .getClient(cURL)
            .create(WebServiceApi.class);

    Call<WsViagemResponse> call = apiService.sendViagem3(
            createPartFromString("fw_addViagem"),
            createPartFromString(parametros),
            file1
    );

@NonNull
private MultipartBody.Part prepareFilePart(String fileName, byte[] content) {

    RequestBody requestFile =
                   RequestBody.create(MediaType.parse("image/*"), content);

    return MultipartBody.Part.createFormData("file", fileName, requestFile);
}

@NonNull
private RequestBody createPartFromString(String descriptionString) {
    return RequestBody.create(
            MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
}

to the dependencies

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.github.anshulagarwal06:Simplify-Permissions:v1'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

I don’t know what could be wrong since in the logcat I see all the requisition being made

D/OkHttp: fw_addViagem
D/OkHttp: --55fa9778-7456-403f-be6b-48ab3c386c9b
D/OkHttp: Content-Disposition: form-data; name="parametros"
D/OkHttp: Content-Transfer-Encoding: binary
D/OkHttp: Content-Type: multipart/form-data; charset=utf-8
D/OkHttp: Content-Length: 82
D/OkHttp: 1|3|2|06/01/2017 17:38:55|01/02/1900 00:00:00|01/02/1900
D/OkHttp: --55fa9778-7456-403f-be6b-48ab3c386c9b
D/OkHttp: Content-Disposition: form-data; name="file";   filename="arquivo1.jpg"
D/OkHttp: Content-Type: image/*
D/OkHttp: Content-Length: 16053

comes after this log the image and at the end

D/OkHttp: <-- 200 OK http://10.0.10.25:8082/webrun/fw_file.rule (111ms)

1 answer

1

The problem here has to do with the parameters of my Server, it expects three whose names are: method, parameter and file

The interface has the three but in it we do not name the last

@Part MultipartBody.Part images

The solution is in the prepareFilePart method, where before I had "file" I must exchange for "file" matching the expected parameter

@NonNull
private MultipartBody.Part prepareFilePart(String fileName, byte[] content) {

        RequestBody requestFile =
               RequestBody.create(MediaType.parse("image/*"), content);

        return MultipartBody.Part.createFormData("arquivo", fileName, requestFile);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.