What are the correct places for parameters in OKHttp in a multi-line call?

I am trying to replicate a jpg file upload using OKHttp to a multi-page form on a PHP server. I believe that I have some parameters in the wrong place, I have no familiarity with multipart forms in http and nomenclature.

Here I am trying to execute

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

Then I make a multiple form request with the POST method with the following: path: https://www.somesite.com/jpgphotoupload.php

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

mimeType: image / jpeg (I understand that)

Not sure where the following pairs of name values ​​should be placed as part of a multiple form request, try addFormDataPart

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

name: imagefile

file_name: myname.jpg

Also here, what else might be appropriate

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

Here is the code I have.

 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; } 
+7
android php forms
source share
1 answer

In my case, I needed to upload the video to the 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(); 
+9
source

All Articles