-
-
Save droid-ash/5da56f18f8bd2d56eddcd7f3871c81bd to your computer and use it in GitHub Desktop.
This file contains 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 Request(internal val method: String) { | |
internal val header: MutableMap<String, String> = HashMap() | |
internal var url: String? = null | |
internal var body: ByteArray? = null | |
private var jsonObjReqListener: JSONObjectListener? = null | |
private var threadExecutor: ThreadExecutor = ThreadExecutor() | |
fun url(url: String?): Request { | |
this.url = url | |
return this | |
} | |
fun body(bodyJson: JSONObject?): Request { | |
val textBody = bodyJson?.toString() | |
body = textBody?.toByteArray(charset(UTF_8)) | |
this.header["Content-Type"] = "application/json" | |
return this | |
} | |
fun header(header: Map<String, String>?): Request { | |
if (header.isNullOrEmpty()) return this | |
this.header.putAll(header) | |
return this | |
} | |
fun makeRequest(jsonObjectListener: JSONObjectListener?): Request { | |
this.jsonObjReqListener = jsonObjectListener | |
threadExecutor.execute(RequestTask(this)) | |
return this | |
} | |
internal fun sendResponse(resp: Response?, e: Exception?) { | |
if (jsonObjReqListener != null) { | |
if (e != null) jsonObjReqListener?.onFailure(e) | |
else jsonObjReqListener?.onResponse(resp?.asJSONObject()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment