An API I'm calling using a non-json format. In the response, I get something like this.
field1=value1&field2=value2&field3=value3
What is this format called? It looks exactly like what you would send in a query usually. I would like to parse this response using Retrofit.
Any ideas? Thank you.
Here is an extension function to parse it into a map. After that it's easy to grab the fields by doing map["field1]
for example.
fun String.parseQueryFormat() : Map<String, String>{
val queryPairs: MutableMap<String, String> = LinkedHashMap()
val pairs = this.split("&").toTypedArray()
for (pair in pairs) {
val idx = pair.indexOf("=")
queryPairs[URLDecoder.decode(pair.substring(0, idx), "UTF-8")] = URLDecoder.decode(pair.substring(idx + 1), "UTF-8")
}
return queryPairs
}
.
val responseString = responseBody.string()
val map = responseString.parseQueryFormat()
val field1 = map["field1"]
val field2 = map["field2"]