@@ -9,50 +9,47 @@ package com.owncloud.android.lib.common.utils.responseFormat
99
1010import com.owncloud.android.lib.common.utils.Log_OC
1111import org.json.JSONArray
12+ import org.json.JSONException
1213import org.json.JSONObject
1314import java.io.ByteArrayInputStream
1415import javax.xml.parsers.DocumentBuilderFactory
1516
1617object ResponseFormatDetector {
1718 private const val TAG = " ResponseFormatDetector"
18- private const val JSON_OBJECT_PREFIX = " {"
19- private const val JSON_ARRAY_PREFIX = " ["
2019
2120 fun detectFormat (input : String ): ResponseFormat =
2221 when {
23- isJson (input) -> ResponseFormat .JSON
24- isXml (input) -> ResponseFormat .XML
22+ isJSON (input) -> ResponseFormat .JSON
23+ isXML (input) -> ResponseFormat .XML
2524 else -> ResponseFormat .UNKNOWN
2625 }
2726
28- @Suppress(" TooGenericExceptionCaught" )
29- private fun isJson (input : String ): Boolean {
27+ private fun isJSON (input : String ): Boolean {
3028 return try {
31- val trimmed = input.trim()
32- if (trimmed.startsWith(JSON_OBJECT_PREFIX )) {
33- JSONObject (trimmed)
34- } else if (trimmed.startsWith(JSON_ARRAY_PREFIX )) {
35- JSONArray (trimmed)
36- } else {
37- return false
38- }
29+ JSONObject (input)
3930 true
40- } catch (e: Exception ) {
41- Log_OC .e(TAG , " Exception isJson: $e " )
42- false
31+ } catch (e: JSONException ) {
32+ try {
33+ Log_OC .i(TAG , " Info it's not JSONObject: $e " )
34+ JSONArray (input)
35+ true
36+ } catch (e: JSONException ) {
37+ Log_OC .e(TAG , " Exception it's not JSONArray: $e " )
38+ false
39+ }
4340 }
4441 }
4542
4643 @Suppress(" TooGenericExceptionCaught" )
47- private fun isXml (input : String ): Boolean =
44+ private fun isXML (input : String ): Boolean =
4845 try {
4946 val factory = DocumentBuilderFactory .newInstance()
5047 val builder = factory.newDocumentBuilder()
5148 val stream = ByteArrayInputStream (input.toByteArray())
5249 builder.parse(stream)
5350 true
5451 } catch (e: Exception ) {
55- Log_OC .e(TAG , " Exception isXml : $e " )
52+ Log_OC .e(TAG , " Exception isXML : $e " )
5653 false
5754 }
5855}
0 commit comments