Image arrives null on the server

1

I am sending data using the retrofit, I observe the entire 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 texts the text arrives correctly, without problems however the image does not , I've already changed the code, I've looked at tutorials, initially I was sending a list of images, I've changed I'm just sending one more this is not the problem my server is in Java

The whole process begins by getting the camera image, 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 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);
}

for 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 do not know what could be wrong since in logcat I see the entire request 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

After this log the image and at the end

D/OkHttp: <-- 200 OK http://10.0.10.25:8082/webrun/fw_file.rule (111ms)
    
asked by anonymous 10.01.2017 / 21:18

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 should change to "file" by marrying 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);
    }
    
12.01.2017 / 00:20