Skip to content

Commit

Permalink
add exception handles in api service
Browse files Browse the repository at this point in the history
  • Loading branch information
creative-dev-lab committed Apr 5, 2022
1 parent c15b85e commit 38ca90e
Showing 1 changed file with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.airrobe.widgetsdk.airrobewidget.service
import com.airrobe.widgetsdk.airrobewidget.BuildConfig
import org.json.JSONObject
import java.io.*
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
Expand All @@ -13,7 +14,6 @@ internal object AirRobeApiService {
const val POST: String = "POST"
private val userHeaderString: String = "airrobeWidget/${BuildConfig.VERSION_NAME} (Android ${android.os.Build.VERSION.RELEASE})"

@Throws(IOException::class)
fun requestPOST(r_url: String?, postDataParams: JSONObject): String? {
val url = URL(r_url)
val urlConnection: HttpURLConnection = url.openConnection() as HttpURLConnection
Expand All @@ -29,52 +29,61 @@ internal object AirRobeApiService {
writer.flush()
writer.close()
os.close()
val responseCode: Int = urlConnection.responseCode // To Check for 200
if (responseCode == HttpsURLConnection.HTTP_OK) {
val `in` = BufferedReader(InputStreamReader(urlConnection.inputStream))
val sb = StringBuffer("")
var line: String?
while (`in`.readLine().also { line = it } != null) {
sb.append(line)
break
}
`in`.close()
return sb.toString()
return try {
val responseCode: Int = urlConnection.responseCode // To Check for 200
if (responseCode == HttpsURLConnection.HTTP_OK) {
val `in` = BufferedReader(InputStreamReader(urlConnection.inputStream))
val sb = StringBuffer("")
var line: String?
while (`in`.readLine().also { line = it } != null) {
sb.append(line)
break
}
`in`.close()
sb.toString()
} else null
} catch (e: IOException) {
return null
}
return null
}

@Throws(IOException::class)
fun requestGET(url: String?): String? {
val obj = URL(url)
val urlConnection = obj.openConnection() as HttpURLConnection
urlConnection.requestMethod = GET
val responseCode = urlConnection.responseCode
return if (responseCode == HttpURLConnection.HTTP_OK) {
val `in` = BufferedReader(InputStreamReader(urlConnection.inputStream))
var inputLine: String?
val response = StringBuffer()
while (`in`.readLine().also { inputLine = it } != null) {
response.append(inputLine)
}
`in`.close()
response.toString()
} else null
return try {
val responseCode = urlConnection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
val `in` = BufferedReader(InputStreamReader(urlConnection.inputStream))
var inputLine: String?
val response = StringBuffer()
while (`in`.readLine().also { inputLine = it } != null) {
response.append(inputLine)
}
`in`.close()
response.toString()
} else null
} catch (e: IOException) {
null
}
}

@Throws(java.lang.Exception::class)
private fun encodeParams(params: JSONObject): String? {
val result = StringBuilder()
var first = true
val itr = params.keys()
while (itr.hasNext()) {
val key = itr.next()
val value = params[key]
if (first) first = false else result.append("&")
result.append(URLEncoder.encode(key, "UTF-8"))
result.append("=")
result.append(URLEncoder.encode(value.toString(), "UTF-8"))
return try {
while (itr.hasNext()) {
val key = itr.next()
val value = params[key]
if (first) first = false else result.append("&")
result.append(URLEncoder.encode(key, "UTF-8"))
result.append("=")
result.append(URLEncoder.encode(value.toString(), "UTF-8"))
}
result.toString()
} catch (e: Exception) {
null
}
return result.toString()
}
}

0 comments on commit 38ca90e

Please sign in to comment.