mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-06 07:29:10 +00:00
Replace Gson with Moshi
This commit is contained in:
@@ -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'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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()
|
||||||
|
)!!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user