mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-06 07:29:10 +00:00
Add DataFetcher class
This commit is contained in:
@@ -45,4 +45,6 @@ dependencies {
|
|||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
androidTestImplementation 'androidx.test:runner:1.2.0'
|
androidTestImplementation 'androidx.test:runner:1.2.0'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||||
|
implementation 'com.google.code.gson:gson:2.8.5'
|
||||||
|
implementation 'org.jsoup:jsoup:1.12.1'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="net.underdesk.circolapp">
|
package="net.underdesk.circolapp">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package net.underdesk.circolapp.server
|
||||||
|
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import net.underdesk.circolapp.server.pojo.Response
|
||||||
|
import org.jsoup.Jsoup
|
||||||
|
import java.io.IOException
|
||||||
|
import java.net.URL
|
||||||
|
import javax.net.ssl.HttpsURLConnection
|
||||||
|
|
||||||
|
class DataFetcher {
|
||||||
|
companion object {
|
||||||
|
const val ENDPOINT_URL = "https://www.curiepinerolo.edu.it/wp-json/wp/v2/pages/5958"
|
||||||
|
|
||||||
|
val gson = Gson()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun getCircularsFromServer(): ArrayList<Pair<String, String>> {
|
||||||
|
val json = gson.fromJson(retrieveDataFromServer(), Response::class.java)
|
||||||
|
|
||||||
|
val document = Jsoup.parseBodyFragment(json.content!!.rendered)
|
||||||
|
val htmlList = document.getElementsByTag("ul")[0].getElementsByTag("a")
|
||||||
|
|
||||||
|
val list = ArrayList<Pair<String, String>>()
|
||||||
|
|
||||||
|
htmlList.forEach { element ->
|
||||||
|
list.add(Pair(element.text(), element.attr("href")))
|
||||||
|
}
|
||||||
|
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun retrieveDataFromServer(): String? {
|
||||||
|
var connection: HttpsURLConnection? = null
|
||||||
|
return try {
|
||||||
|
connection = (URL(ENDPOINT_URL).openConnection() as? HttpsURLConnection)
|
||||||
|
connection?.run {
|
||||||
|
// Set GET HTTP method
|
||||||
|
requestMethod = "GET"
|
||||||
|
|
||||||
|
setRequestProperty("Accept-Encoding", "none")
|
||||||
|
|
||||||
|
connect()
|
||||||
|
if (responseCode != HttpsURLConnection.HTTP_OK) {
|
||||||
|
throw IOException("HTTP error code: $responseCode")
|
||||||
|
}
|
||||||
|
|
||||||
|
inputStream?.reader()?.readText()
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
// Close Stream and disconnect HTTPS connection.
|
||||||
|
connection?.inputStream?.close()
|
||||||
|
connection?.disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.underdesk.circolapp.server.pojo
|
||||||
|
|
||||||
|
data class Content(
|
||||||
|
val rendered: String? = null
|
||||||
|
)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.underdesk.circolapp.server.pojo
|
||||||
|
|
||||||
|
data class Response(
|
||||||
|
val content: Content? = null
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user