0

When I upload file by okhttp use the following way

version : com.squareup.okhttp3:okhttp:3.12.0

OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();   
OkHttpClient okHttpClient = clientBuilder.build();
MultipartBody.Builder builder = new MultipartBody
        .Builder()
        .setType(MultipartBody.FORM);
for (int i = 0; i < fileList.size(); i++) {
        File file = new File(fileList.get(i));
        builder.addFormDataPart(path, path, 
        RequestBody.create(MediaType.parse("application/octet-stream"), file));
}
RequestBody body = builder.build();
final Request request = new Request.Builder()
        .url(upLoadUrl)
        .post(body)
        .build();
okHttpClient.newCall(request).enqueue(new Callback() {})

and I get response in method onResponse()

The background server does not do any processing on the image, just returns a link.

Sometimes the pictures I get are complete and sometimes distorted.

Like these :

enter image description here

Is my method of use wrong?

I also make MediaType MediaType.parse("image/*") or MediaType.parse("image/png") or MediaType.parse("image/jpeg"), also didn't work.

halfer
  • 19,824
  • 17
  • 99
  • 186
LaymanZ
  • 51
  • 2

1 Answers1

0

I found a solution, just write the okhttpclient object as a singleton.

LaymanZ
  • 51
  • 2