object HttpCall {
private val MEDIA_TYPE_PNG = MediaType.parse("image/png");
private val HEAD_PNG = Headers.of("Content-Disposition", "form-data; filename=\"img.png\"");
private val okclient = OkHttpClient()
fun post(context: Context, url: String, params: Map<String, String>?, files: Map<String, List<File>>?, callback: Callback): Call {
val builder = MultipartBody.Builder()
.setType(MultipartBody.FORM)
val paramsF = params.orEmpty()
val filesF = files.orEmpty()
for ((key, value) in paramsF) {
builder.addFormDataPart(key, value)
}
for ((key, value) in filesF) {
if (value.isEmpty()) continue
val filesBuilder = MultipartBody.Builder();
for (oneFile in value) {
filesBuilder.addPart(HEAD_PNG, RequestBody.create(MEDIA_TYPE_PNG, oneFile))
}
builder.addFormDataPart(key, null, filesBuilder.build())
}
val formBody = builder.build()
val request = Request.Builder()
.url(url)
.post(formBody)
.build();
val call = okclient.newCall(request);
if (callback is GsonHttpCallback<*>) callback.onStart()
call.enqueue(callback);
return call;
}
}