Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct places for parameters in OKHttp in a multipart form call?

I'm trying to replicate a jpg file upload using OKHttp to a multipart form on a PHP server. I believe, I got some of the parameters in the wrong place, I don't have the familiarity with multipart forms in http and the nomenclature.

Here's what I'm trying to accomplish

Post the parameters (name value pairs): myuser, token, types to https://www.somesite.com/jpgphotoupload.php

I'm then making a multipart form request with method POST, with the following: path: https://www.somesite.com/jpgphotoupload.php

file data: JPEG compressed image data of size 480 x 640 (This I understand)

mimeType: image/jpeg (This I understand)

Not sure where the following name value pairs should be placed as part of the multipart form request, tried addFormDataPart

parameters: again the parameter form above, (myuser, token, types)

name: imagefile

fileName: myname.jpg

In addition, here's what else may be pertinent

            "Connection" , "Keep-Alive"
            "ENCTYPE", "multipart/form-data"
            "Content-Type", "multipart/form-data"

Here's the code that I have currently.

MediaType MEDIA_TYPE_JPG = MediaType.parse("image/jpg");

            OkHttpClient client = new OkHttpClient();
                RequestBody requestBody = new MultipartBuilder()
                        .type(MultipartBuilder.FORM)
                        .addPart(
                                Headers.of("Content-Disposition", "form-data; name=\"imagefile\""),
                                RequestBody.create(MEDIA_TYPE_JPG, new File("/storage/emulated/0/download/camerapic.jpg")))
                        .addFormDataPart("myuser", getprefmyuser(getBaseContext()))
                        .addFormDataPart("token", getpreftoken(getBaseContext()))
                        .addFormDataPart("types", "type1")
                        .addFormDataPart("fileName", "myname.jpg")
                        .build();

                Request request = new Request.Builder()
                        .header("myuser", getprefmyuser(getBaseContext()))
                        .header("token", getpreftoken(getBaseContext()))
                        .header("type", "car")
                        .url("https://www.somesite.com/jpgphotoupload.php")
                        .post(requestBody)
                        .build();

            Response response = null;
            try {
                response = client.newCall(request).execute();
            } catch (IOException e) {
                e.printStackTrace(); 
        ... 

        return null;
        }
like image 427
Peter Birdsall Avatar asked Mar 23 '15 21:03

Peter Birdsall


People also ask

What is OkHttp in Java?

OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features, such as connection pooling (if HTTP/2 isn't available), transparent GZIP compression, and response caching, to avoid the network completely for repeated requests.

What is the use of OkHttp in Android?

OkHttp android provides an implementation of HttpURLConnection and Apache Client interfaces by working directly on a top of java Socket without using any extra dependencies.

Is OkHttp async?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.

What is Okhttpclient in Kotlin?

OkHttp is a third-party library developed by Square for sending and receive HTTP-based network requests. It is built on top of the Okio library, which tries to be more efficient about reading and writing data than the standard Java I/O libraries by creating a shared memory pool.


1 Answers

In my case, I needed to upload a video to an Amazon S3 bucket. This is what worked for me.

File sourceFile = new File(myUri);

RequestBody requestBody = new MultipartBuilder()
                    .type(MultipartBuilder.FORM)
                    .addFormDataPart("keyOne", "valueOne")
                    .addFormDataPart("keyTwo", "valueTwo")
                    .addFormDataPart("file", "myFileName", RequestBody.create(MediaType.parse("video/quicktime"), sourceFile))
                    .build();
like image 124
Mike Cole Avatar answered Sep 28 '22 08:09

Mike Cole