Last active
September 6, 2018 07:16
-
-
Save Audhil/fc76c8e7ede4b0d7b97b7d52b38a2a9f to your computer and use it in GitHub Desktop.
multi-part request (uploading an Image) with HttpURLConnection - age old method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dummy { | |
// make urlConnection -> multipart | |
var attachmentName = "content" | |
var attachmentFileName = "profile.jpeg" | |
var crlf = "\r\n" | |
var twoHyphens = "--" | |
var boundary = "*****" | |
var iStream: InputStream? = null | |
// multi part request | |
fun makeAMultiPartRequest(imageBytes: ByteArray, token: String): String? { | |
val url: URL? = | |
when { | |
ConstantsUtil.USER_BD.readStringFromPref() == ConstantsUtil.BD_US -> | |
URL(END_POINT_US + UPLOAD_PHOTO_URL) | |
ConstantsUtil.USER_BD.readStringFromPref() == ConstantsUtil.BD_EU -> | |
URL(END_POINT_EU + UPLOAD_PHOTO_URL) | |
ConstantsUtil.USER_BD.readStringFromPref() == ConstantsUtil.BD_IND -> | |
URL(END_POINT_IND + UPLOAD_PHOTO_URL) | |
ConstantsUtil.USER_BD.readStringFromPref() == ConstantsUtil.BD_CHN -> | |
URL(END_POINT_CHN + UPLOAD_PHOTO_URL) | |
else -> | |
URL(END_POINT_US + UPLOAD_PHOTO_URL) | |
} | |
showVLog("IMAGE_UPLOAD url?.host: ${url?.host}") | |
val httpUrlConnection = url?.openConnection() as HttpURLConnection | |
httpUrlConnection.useCaches = false | |
httpUrlConnection.doOutput = true | |
httpUrlConnection.requestMethod = "POST" | |
httpUrlConnection.setRequestProperty(ConstantsUtil.USER_AGENT, WorkerlyDelegate.appUserAgent) | |
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive") | |
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache") | |
httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=$boundary") | |
httpUrlConnection.setRequestProperty(ConstantsUtil.AUTHORIZATION, token) | |
httpUrlConnection.connect() | |
val request = DataOutputStream(httpUrlConnection.outputStream) | |
request.writeBytes(twoHyphens + boundary + crlf) | |
request.writeBytes("Content-Disposition: form-data; name=\"" + | |
attachmentName + "\";filename=\"" + | |
attachmentFileName + "\"" + crlf) | |
request.writeBytes(crlf) | |
request.write(imageBytes) | |
request.writeBytes(crlf) | |
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf) | |
request.flush() | |
request.close() | |
// get response | |
val responseCode = httpUrlConnection.responseCode | |
showVLog("IMAGE_UPLOAD responseCode :$responseCode") | |
if (responseCode != 200) { | |
iStream = httpUrlConnection.errorStream | |
val response = readIStream(iStream) | |
showVLog("IMAGE_UPLOAD ResponseCode != 200 response:$response") | |
return null | |
} | |
iStream = httpUrlConnection.inputStream | |
val response = readIStream(iStream) | |
showVLog("IMAGE_UPLOAD ResponseCode == 200 response:$response") | |
return response | |
} | |
// read I stream | |
private fun readIStream(iStream: InputStream?): String? { | |
var reponseIs = ConstantsUtil.EMPTY | |
iStream?.bufferedReader()?.useLines { lines -> | |
lines.forEach { | |
reponseIs += it | |
} | |
} | |
return reponseIs | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// API call from Android side | |
Observable.create<String> { emitter -> | |
try { | |
emitter.onNext(makeAMultiPartRequest(imageBytes, token)!!) | |
emitter.onComplete() | |
} catch (e: Exception) { | |
e.printStackTrace() | |
emitter.tryOnError(e) | |
} | |
}.compose(AppRxSchedulers.applySchedulers()) | |
.subscribe( | |
{ | |
onNext | |
println("---IMAGE_UPLOAD it is : onNext: it:$it") | |
}, | |
{ | |
onError | |
println("---IMAGE_UPLOAD it is : onError: it.message:${it.message}") | |
}, | |
{ | |
onComplete | |
println("---IMAGE_UPLOAD it is : onComplete") | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment