-2

I am using put request to upload image and to the server using Okhttp, the request work fine with the postman, but through android it is giving me bad request i have implemented the method discussed here https://stackoverflow.com/a/23784452/9145387

Kindly help me out sorting this.... Thanks in Advance

 public  Boolean uploadFile(String serverURL, File file) {
    OkHttpClient client = new OkHttpClient();
    try {

        RequestBody requestBody = new MultipartBuilder()
                .type(MultipartBuilder.FORM)
                .addFormDataPart("file", file.getName(),
                        RequestBody.create(MediaType.parse("image/jpg"), file))
                .addFormDataPart(caseId+"_audio", caseId+"_audio",
                        RequestBody.create(MediaType.parse("audio/*"), Audio))
                .build();

        Request request = new Request.Builder()
                .url(serverURL)
                .put(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                e.printStackTrace();
                // Handle the error
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (!response.isSuccessful()) {
                    response.toString();
                    // Handle the error
                }
                // Upload successful
            }
        });
    }catch (Exception ex) {
        ex.printStackTrace();
        // Handle the error
    }
    return false;
}

screenshot of the successful upload of image on postman

Manzoor Ahmad
  • 481
  • 6
  • 21
  • Put log into the first position of onFailure, onResponse methods to see whether it go into that or not. Just something like Log.i(TAG, "gotothis"); – Cao Minh Vu Mar 27 '18 at 09:33
  • i debugged the code, after executing this line client.newCall(request).enqueue(new Callback() {} it directly goes to return false. – Manzoor Ahmad Mar 27 '18 at 09:36
  • You mean it will return "false" immediately when you call the function: uploadFile ? – Cao Minh Vu Mar 27 '18 at 09:37
  • yes it immediately goes to return false. – Manzoor Ahmad Mar 27 '18 at 09:38
  • It must be, since this code: client.newCall(request).enqueue will be executed asynchronously. Hence you have to wait for a while to get response from onFailure or onResponse. – Cao Minh Vu Mar 27 '18 at 09:40
  • well i have waited for quite long, i have added the breakpoints on the onFailure and on Responce but it doesn't seem to get executed. – Manzoor Ahmad Mar 27 '18 at 09:41
  • **[obviously you should learn about async calls and multithreading](https://ideone.com/PPHi95)** ... your return statment in this example is 3. ... and onResponse is 2. - as you see obviously program would go to 3 before 2... – Selvin Mar 27 '18 at 09:42
  • Please try to use log to see if there is any difference since I do not know whether you set up breakpoint correctly – Cao Minh Vu Mar 27 '18 at 09:42
  • got it... thanks Cao Minh ... it was getting timeout error... can you tell me how can i extend request time ? – Manzoor Ahmad Mar 27 '18 at 09:45
  • Try Using `MediaType.parse("image/*")`. You are passing .png instead of .jpg. – Pragnesh Ghoda シ Mar 27 '18 at 09:46
  • hey anyone there, the code is giving me bad request, while with the postman it is working fine. – Manzoor Ahmad Mar 28 '18 at 08:30

2 Answers2

0

Try this to reset timeout:

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(60, TimeUnit.SECONDS); 
client.setReadTimeout(60, TimeUnit.SECONDS);
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
0

well the error was in the request body.

   RequestBody requestBody = new MultipartBuilder()
            .type(MultipartBuilder.FORM)
            .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse("image/jpg"), file))
            .addFormDataPart(caseId+"_audio", caseId+"_audio",
                    RequestBody.create(MediaType.parse("audio/*"), Audio))
            .build();

Changed this line

 .addFormDataPart(caseId+"_audio", caseId+"_audio",
                    RequestBody.create(MediaType.parse("audio/*"), Audio))
            .build();

to

   .addFormDataPart(caseId+"_audio", Audio.getName(),
                    RequestBody.create(MediaType.parse("audio/*"), Audio))
            .build();
Manzoor Ahmad
  • 481
  • 6
  • 21