Put image with retrofit or okhttp3? I do not know how to do it

0

I'm trying to make a put with retrofit and also directly with okhttp3 and I could not do it with any

The codes I have used are the following.

Okhttp3:

public void uploadFile() {

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {


                File file = new File(urlAudio);

                Interceptor interceptor = new Interceptor() {
                    @Override
                    public okhttp3.Response intercept(Chain chain) throws IOException {
                        final Request request = chain.request().newBuilder()
                                .addHeader("uid", Constantes.uid)
                                .addHeader("access-token", Constantes.token)
                                .addHeader("expiry", Constantes.expiry)
                                .addHeader("client", Constantes.client)
                                .build();

                        return chain.proceed(request);
                    }

                };


                //ProgressListenerPool pool = new ProgressListenerPool();
                OkHttpClient client = new OkHttpClient.Builder()
                        .readTimeout(30, TimeUnit.SECONDS)
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .addInterceptor(interceptor)
                        .build();

                RequestBody formBody = new FormBody.Builder()
                        .add("patient_info_file", "")
                        .build();

                RequestBody requestFile = RequestBody.create(MediaType.parse("audio/3gp"), file);
                RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("patient_info_file", file.getName(), requestFile)
                        .addPart(formBody)
                        .build();

                Request request = new Request.Builder()
                        .url(Constantes.BASE_URL + "patients/168")
                        .put(requestBody)
                        .build();

                okhttp3.Response response = null;
                try {
                    response = client.newCall(request).execute();
                    Log.e("ilogica", "register: " + response.body().string());
                    if (response.isSuccessful()) {
                        Log.e("ilogica", "Pacientes enviados " + response.body().toString());

                    } else {
                        Log.e("ilogica", "ERROR: Okhttp3");
                        Log.e("ilogica", "ERROR: RESPONSE: " + response.code());

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

    }

With retrofit:

@Headers("Content-Type: application/json")
@PUT("patients/{patient_id}")
Call<AddPatientResponse> updateAudio(@Path("patient_id") String patient_id, @Body AddAudioRequest request);

And this from the class

private static OkHttpClient getRequestHeader(final String uid, final String token, final String expiry, final String client) {

        Interceptor interceptor = new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                final Request request = chain.request().newBuilder()
                        .addHeader("uid", uid)
                        .addHeader("access-token", token)
                        .addHeader("expiry", expiry)
                        .addHeader("client", client)
                        .build();

                return chain.proceed(request);
            }
        };



      //ProgressListenerPool pool = new ProgressListenerPool();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .build();

        return okHttpClient;
    }


public void subirAudio() {

    RequestBody requestBody;

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constantes.BASE_URL)
            .client(getRequestHeader(Constantes.uid, Constantes.token, Constantes.expiry, Constantes.client))
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    ApiService apiService = retrofit.create(ApiService.class);

    File file = new File(urlAudio);


    RequestBody requestFile = RequestBody.create(MediaType.parse("audio/3gp"), file);
    requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("patient_info_file", file.getName(), requestFile)
            .build();

    AddAudioRequest addAudioRequest = new AddAudioRequest(requestBody);

    Call<AddPatientResponse> call = service.updateAudio("168", requestBody);
    call.enqueue(new Callback<AddPatientResponse>() {
        @Override
        public void onResponse(Call<AddPatientResponse> call, Response<AddPatientResponse> response) {
            if (response.isSuccessful()) {
                Log.e("ilogica", "Pacientes enviados " + response.body().toString());


            } else {
                Log.e("ilogica", "Pacientes fallados " + response.message().toString());
                Log.e("ilogica", "Pacientes fallados " + response.toString());
            }
        }

        @Override
        public void onFailure(Call<AddPatientResponse> call, Throwable t) {

        }
    });

}

I have also tried to generate a body directly in the interface and it gives me an error

    
asked by Jimmy Hernández 20.09.2017 в 17:54
source

0 answers