Skip to content

Instantly share code, notes, and snippets.

@ariefbayu
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariefbayu/4c5c50ee3f9441a9fc1a to your computer and use it in GitHub Desktop.
Save ariefbayu/4c5c50ee3f9441a9fc1a to your computer and use it in GitHub Desktop.
OkHttp recipe for uploading files with additional form inputs
File file1 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Master/CAM_" + s.getType() + "_" + s.getJobCode() + ".jpg");
File file2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Master/SIGN_" + s.getType() + "_" + s.getJobCode() + ".jpg");
Log.d("LOG", "File1: "+ (file1.exists() ? "E" : "N") + " -> " + file1.getAbsolutePath());
Log.d("LOG", "File2: "+ (file2.exists() ? "E" : "N") + " -> " + file2.getAbsolutePath());
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("gambar1", file1.getName(),
RequestBody.create(MEDIA_TYPE_IMAGE, file1))
.addFormDataPart("gambar2", file2.getName(),
RequestBody.create(MEDIA_TYPE_IMAGE, file2))
.addFormDataPart("job", s.getJobCode())
.addFormDataPart("auth", dp.getLoginAuth())
.addFormDataPart("lat", String.valueOf(s.getLatitude()))
.addFormDataPart("lon", String.valueOf(s.getLongitude()))
.addFormDataPart("acc", String.valueOf(s.getAccuracy()))
.addFormDataPart("tipe", s.getType())
.addFormDataPart("tanggal", s.getTanggal())
.addFormDataPart("pemberi", s.getPemberi())
.addFormDataPart("penerima", s.getPenerima())
.addFormDataPart("penandatangan", s.getPenandatangan())
.addFormDataPart("catatan", s.getCatatan())
.addFormDataPart("rate", String.valueOf(s.getRate()))
.build();
Request request = new Request.Builder().url(URL_UPLOAD_SIGNATURE).post(requestBody).build();
Response response = client.newCall(request).execute();
Log.d("LOG", s.getType() + " Resonse: " + response.body().string());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment