The solution presented in this code snippet uses built-in Android DocumentBuilder to process the XML. This class is available in Android since API Level 1. In the following sample, the application needs to fetch a list of photos from Flickr service. This service provides the XML formatted response. We are only interested in populating our FlickrPhotoModel data classes with the attributes from <photo> nodes.
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
import android.net.Uri | |
import com.commonsound.common.data.flickr.FlickrApi.Routes.FLICKR_REST_SERVICE | |
import com.commonsound.common.data.flickr.models.FlickrPhotoModel | |
import io.ktor.client.* | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.withContext | |
import org.w3c.dom.NodeList | |
import java.io.BufferedInputStream | |
import java.io.BufferedReader | |
import java.net.URL | |
import javax.net.ssl.HttpsURLConnection | |
import javax.xml.parsers.DocumentBuilder | |
import javax.xml.parsers.DocumentBuilderFactory | |
data class FlickrPhotoModel( | |
var id: String, | |
var server: String, | |
var secret: String | |
) | |
interface FlickrApi { | |
suspend fun getPhotos(tag: String): List<FlickrPhotoModel> | |
companion object Routes { | |
private const val BASE_FLICKR_URL = "https://www.flickr.com/" | |
const val FLICKR_REST_SERVICE = "${BASE_FLICKR_URL}services/rest" | |
} | |
} | |
class FlickrApiImpl(private val client: HttpClient): FlickrApi { | |
override suspend fun getPhotos(tag: String): List<FlickrPhotoModel> { | |
return withContext(Dispatchers.IO) { | |
val encodedQuery = Uri.Builder() | |
.appendQueryParameter("tags", tag) | |
.appendQueryParameter("method", "flickr.photos.search") | |
.appendQueryParameter("api_key", "some_key") | |
.appendQueryParameter("per_page", "5") | |
.build().encodedQuery | |
val urlConnection = URL("$FLICKR_REST_SERVICE?$encodedQuery") | |
.openConnection() as HttpsURLConnection | |
try { | |
urlConnection.requestMethod = "GET" | |
val inStream = BufferedInputStream(urlConnection.getInputStream()) | |
val content = BufferedReader(inStream.reader()).readText() | |
parseFlickrPhotosXml(content) | |
} finally { | |
urlConnection.disconnect() | |
} | |
} | |
} | |
private fun parseFlickrPhotosXml(s: String): List<FlickrPhotoModel> { | |
val builder: DocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder() | |
val photoNodes: NodeList = builder.parse(s.byteInputStream()).getElementsByTagName("photo") | |
val result = mutableListOf<FlickrPhotoModel>() | |
for (p in 0 until photoNodes.length) { | |
val photo = photoNodes.item(p) | |
result.add(FlickrPhotoModel( | |
photo.attributes.getNamedItem("id").nodeValue, | |
photo.attributes.getNamedItem("server").nodeValue, | |
photo.attributes.getNamedItem("secret").nodeValue | |
)) | |
} | |
return result | |
} | |
} |
While this approach may not be ideal if we need to work extensively with XML API, it is a proper fallback to use for just a bunch of requests.
No comments :
Post a Comment