mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-30 14:49:47 +02:00
Fix:Catch api handler invalid json response body
This commit is contained in:
parent
066b72d54d
commit
d9cdc16ff9
1 changed files with 15 additions and 7 deletions
|
@ -17,6 +17,7 @@ import com.getcapacitor.JSObject
|
||||||
import okhttp3.*
|
import okhttp3.*
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
import okhttp3.MediaType.Companion.toMediaType
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
@ -106,14 +107,21 @@ class ApiHandler(var ctx:Context) {
|
||||||
if (bodyString == "OK") {
|
if (bodyString == "OK") {
|
||||||
cb(JSObject())
|
cb(JSObject())
|
||||||
} else {
|
} else {
|
||||||
var jsonObj = JSObject()
|
try {
|
||||||
if (bodyString.startsWith("[")) {
|
var jsonObj = JSObject()
|
||||||
val array = JSArray(bodyString)
|
if (bodyString.startsWith("[")) {
|
||||||
jsonObj.put("value", array)
|
val array = JSArray(bodyString)
|
||||||
} else {
|
jsonObj.put("value", array)
|
||||||
jsonObj = JSObject(bodyString)
|
} else {
|
||||||
|
jsonObj = JSObject(bodyString)
|
||||||
|
}
|
||||||
|
cb(jsonObj)
|
||||||
|
} catch(je:JSONException) {
|
||||||
|
Log.e(tag, "Invalid JSON response ${je.localizedMessage} from body $bodyString")
|
||||||
|
val jsobj = JSObject()
|
||||||
|
jsobj.put("error", "Invalid response body")
|
||||||
|
cb(jsobj)
|
||||||
}
|
}
|
||||||
cb(jsonObj)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue