0

Generated the code below by postman:

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"image\"; filename=\"[object Object]\"\r\nContent-Type: false\r\n\r\n\r\n-----011000010111000001101001--");
Request request = new Request.Builder()
    .url("http://foobar.com/newsfeed/photo")
    .post(body)
    .addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
    .addHeader("x-access-token", "MczCvMEbllNhGaMwEDnGXuQjrwBAYuYleFlgsUZDWRYbVaohpEGgofonYcvHsgPaTnbzHxCvJWalYFTY")
    .addHeader("accept-language", "ru")
    .build();

   Response response = client.newCall(request).execute();

This request make a file on Server with 0 Kb size. and I couldn't put a file. So, I have to put a file like this:

RequestBody body = RequestBody.create(mediaType, new File(filename));

But I got TimeOutExaption.

How to put a file by this kind of Rest API?

Akbar
  • 430
  • 4
  • 18
  • Take a look at my answer here may help u : http://stackoverflow.com/questions/39078192/send-file-to-server-via-retrofit2-as-object – Amir Aug 24 '16 at 11:59
  • I read it @Amir , but It doesn't work for me((( – Akbar Aug 25 '16 at 15:17
  • can you put some relevance part of your code? or some log? Also don't forget as I see your postman, you need some Authentication. – Amir Aug 25 '16 at 15:19
  • I found a way to multipart file upload by [Ion](https://github.com/koush/ion). – Akbar Sep 19 '16 at 05:50

1 Answers1

0

I found a way to multipart file upload by Ion.

Ion.with(context).load(url).setHeader("x-access-token", token)
            .setMultipartParameter("x-access-token", token)
            .setMultipartContentType("multipart/form-data")
            .setMultipartFile("image", "image/jpeg", file)
            .asString().withResponse().setCallback(new FutureCallback<Response<String>>() {
        @Override
        public void onCompleted(Exception e, Response<String> result) {
            if (result != null)
                onSuccess(result.getResult(), result.getHeaders().code());
        }
    });

Rest API: enter image description here

enter image description here

Akbar
  • 430
  • 4
  • 18