0
votes

Hello Stackoverflow's Guys! This is my first post and really is my Big problem with Retrofit 2 I hope Some guys help to solving that.

this is my Interface class:

public interface PicUploadInterface {
@Multipart
@POST("user")
Call<UserModelResponse> uploadMultipleFiles(@Part("Authorization")  RequestBody authorization,
                          @Part("description") RequestBody description,
                          @Part MultipartBody.Part filePart);

and apimanager class:

public class Apimanager {

public static void upload(String coda, File file) {
    // create upload service client
    final String API_BASE_URL = "http://10.0.3.2/web2Demo/public/";
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    httpClient.addInterceptor(logging);  

    Retrofit builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build();

    PicUploadInterface service = builder.create(PicUploadInterface.class);
    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    MultipartBody.Part filePart  = MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile);

    String descriptionString = coda;
    String AuthString = "Send picture";
    RequestBody description = RequestBody.create(MediaType.parse("text/plain"), descriptionString);
    RequestBody authorization = RequestBody.create(MediaType.parse("text/plain"), AuthString);

    Call<UserModelResponse> call = service.uploadMultipleFiles(authorization, description, filePart);
    call.enqueue(new Callback<UserModelResponse>() {
        @Override
        public void onResponse(Call<UserModelResponse> call,
                               Response<UserModelResponse> response) {
            Log.v("Upload", "success");
        }
        @Override
        public void onFailure(Call<UserModelResponse> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });
}

and i using code in this way:

      File file = new File(textViewFile.getText().toString()); // getting this string "/storage/emulated/0/bichooser/1476172212892.jpg"
      connectivity.com.pack.Apimanager.upload(name, file);

and finally my HttpLoggingInterceptor show this that I cannt find what is and how pars it in my restApi

10-11 03:50:12.944 1393-1681/transfer.com.bermou D/pager.tab.component.RegisterFormFragments: onImageChosen: /storage/emulated/0/bichooser/1476172212892.jpg 10-11 03:50:41.236 1393-2102/transfer.com.bermou D/OkHttp: --> POST http://10.0.3.2/web2Demo/public/user http/1.1 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/OkHttp: Content-Type: multipart/form-data; boundary=66e09d68-b88d-4ace-b978-a56e1a6ab66b 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/OkHttp: Content-Length: 59914 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;) 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;) 10-11 03:50:41.244 1393-2102/transfer.com.bermou I/dalvikvm: Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm: VFY: unable to resolve static method 35770: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream; 10-11 03:50:41.244 1393-2102/transfer.com.bermou D/dalvikvm: VFY: replacing opcode 0x71 at 0x000b 10-11 03:50:41.244 1393-2102/transfer.com.bermou W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;) 10-11 03:50:41.248 1393-2102/transfer.com.bermou W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;) 10-11 03:50:41.248 1393-2102/transfer.com.bermou I/dalvikvm: Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source 10-11 03:50:41.248 1393-2102/transfer.com.bermou W/dalvikvm: VFY: unable to resolve static method 35769: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream; 10-11 03:50:41.248 1393-2102/transfer.com.bermou D/dalvikvm: VFY: replacing opcode 0x71 at 0x000b 10-11 03:50:41.268 1393-2102/transfer.com.bermou D/dalvikvm: GC_FOR_ALLOC freed 2130K, 30% free 7056K/9952K, paused 4ms, total 4ms 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: ������JFIF����������������C�� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: �����������}��!1AQa"q2���#B��R��$3br� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������������������
10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: ���������w��!1AQaq"2�B���� #3R�br� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������������?��x�PO�J+�2yr�Ɇ���pVBQv����0\Bs��4� 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: �r�I����TӍ�Js�)(���߻Rq��}l,�q 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: ��]�\I�C��C�#%��e�\�2��5�r�㒝H՚�8ڔe 10-11 03:50:41.272 1393-2102/transfer.com.bermou D/OkHttp: /���i��sn��Vnץ�%(�f�B�S�zr��<�Fu��s�W END POST (59914-byte body) ===================================================================================================================

I can't find what is going out from retrofit 2.1.0 to my restApi and which part is my string or image data? I did change many time based on stackoverflow tips and another site but my result was almost same! ( i did short image binary outgoing from interceptor log) If someone have idea please share that. Thanks

1
what response r u getting in retrofit callback?darwin
indeed my big problem in posting data and in this time response isnot important for me.CodeSqueezer

1 Answers

0
votes

Have you tried using directly in your service call:

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

...

Call<UserModelResponse> call = service.uploadMultipleFiles(authorization, description, requestFile);

And updating this:

@Multipart
@POST("user")
Call<UserModelResponse> uploadMultipleFiles(@Part("Authorization")  RequestBody authorization,
                          @Part("description") RequestBody description,
                          @Part("file\"; filename=\"myFilename\" ") RequestBody filePart);

Another thing, if you want to see your calls to your restApi you could use Charles Proxy as a reverse proxy and see your calls and responses.