Replace Gson with Moshi

This commit is contained in:
2020-09-02 21:01:15 +02:00
committed by Matte23
parent bdb3d954fc
commit 46d471a927
4 changed files with 20 additions and 11 deletions

View File

@@ -55,6 +55,7 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.3.0' androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.squareup.okhttp3:okhttp:4.8.1' implementation 'com.squareup.okhttp3:okhttp:4.8.1'
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.squareup.moshi:moshi:1.9.3'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.9.3'
implementation 'org.jsoup:jsoup:1.13.1' implementation 'org.jsoup:jsoup:1.13.1'
} }

View File

@@ -18,7 +18,7 @@
package net.underdesk.circolapp.server package net.underdesk.circolapp.server
import com.google.gson.Gson import com.squareup.moshi.Moshi
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import net.underdesk.circolapp.data.Circular import net.underdesk.circolapp.data.Circular
@@ -32,16 +32,17 @@ class DataFetcher {
companion object { companion object {
const val ENDPOINT_URL = "https://www.curiepinerolo.edu.it/wp-json/wp/v2/pages/5958" const val ENDPOINT_URL = "https://www.curiepinerolo.edu.it/wp-json/wp/v2/pages/5958"
val gson = Gson() private val moshi = Moshi.Builder().build()
val client = OkHttpClient() private val responseAdapter = moshi.adapter(Response::class.java)
private val client = OkHttpClient()
} }
@Throws(IOException::class) @Throws(IOException::class)
suspend fun getCircularsFromServer(): List<Circular> { suspend fun getCircularsFromServer(): List<Circular> {
return withContext(Dispatchers.Default) { return withContext(Dispatchers.Default) {
val json = gson.fromJson(retrieveDataFromServer(), Response::class.java) val json = retrieveDataFromServer()
val document = Jsoup.parseBodyFragment(json.content!!.rendered) val document = Jsoup.parseBodyFragment(json.content.rendered)
val htmlList = document.getElementsByTag("ul")[0].getElementsByTag("a") val htmlList = document.getElementsByTag("ul")[0].getElementsByTag("a")
val list = ArrayList<Circular>() val list = ArrayList<Circular>()
@@ -54,13 +55,12 @@ class DataFetcher {
list.add(Circular.generateFromString(element.text(), element.attr("href"))) list.add(Circular.generateFromString(element.text(), element.attr("href")))
} }
} }
list list
} }
} }
@Throws(IOException::class) @Throws(IOException::class)
private suspend fun retrieveDataFromServer(): String? { private suspend fun retrieveDataFromServer(): Response {
val request = Request.Builder() val request = Request.Builder()
.url(ENDPOINT_URL) .url(ENDPOINT_URL)
.build() .build()
@@ -72,7 +72,9 @@ class DataFetcher {
throw IOException("HTTP error code: ${response.code})") throw IOException("HTTP error code: ${response.code})")
} }
response.body?.string() responseAdapter.fromJson(
response.body!!.string()
)!!
} }
} }
} }

View File

@@ -1,5 +1,8 @@
package net.underdesk.circolapp.server.pojo package net.underdesk.circolapp.server.pojo
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class Content( data class Content(
val rendered: String? = null val rendered: String
) )

View File

@@ -1,5 +1,8 @@
package net.underdesk.circolapp.server.pojo package net.underdesk.circolapp.server.pojo
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class Response( data class Response(
val content: Content? = null val content: Content
) )