Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ExecutionThread(private val mCustomCallback: CustomCallback) : Thread() {
- override fun run() {
- mCustomCallback.showProgressBar()
- val inputStream = BufferedInputStream(URL(JSON_URL).openStream())
- val url = URL(JSON_URL)
- val connection = url.openConnection()
- connection.connect()
- inputStream.bufferedReader().use { File(FILE_PATH).writeText(it.readLine()) }
- val text = File(FILE_PATH).readText()
- val flightResponse = parseJson(text)
- val flights = flightResponse?.flights
- mCustomCallback.setFlights(flights)
- mCustomCallback.hideProgressBar()
- }
- private fun parseJson(text: String): FlightResponse? {
- var flightResponse: FlightResponse? = null
- try {
- val list = ArrayList<Flight>()
- val jsonArray = JSONArray(text)
- (0 until jsonArray.length())
- .map { jsonArray.getJSONObject(it) }
- .forEach {
- list.add(Flight(it.optInt("flight_number"),
- Rocket(it.optJSONObject("rocket").optString("rocket_id"),
- it.optJSONObject("rocket").optString("rocket_name")),
- it.optString("launch_date_unix"),
- Links(it.optJSONObject("links").optString("mission_patch"),
- it.optJSONObject("links").optString("article_link"),
- it.optJSONObject("links").optString("video_link")),
- it.optString("details")))
- }
- flightResponse = FlightResponse(list)
- } catch (ex: JSONException) {
- ex.printStackTrace()
- }
- return flightResponse
- }
- companion object {
- val JSON_URL = "https://api.spacexdata.com/v2/launches?launch_year=2017"
- val FILE_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path + "/temp.json"
- }
- interface CustomCallback {
- fun setFlights(flights: List<Flight>?)
- fun showProgressBar()
- fun hideProgressBar()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement