Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication
- import android.os.Bundle
- import android.os.Handler
- import android.os.Looper
- import android.support.v7.app.AppCompatActivity
- import android.widget.TextView
- import java.net.URL
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val text1 = findViewById(R.id.text1) as TextView
- val text2 = findViewById(R.id.text2) as TextView
- async { // C# Style
- try {
- text1.text = await(URL("https://fb.com/")
- .readTextAsync()) // Асинхронный запрос
- .getTitle()
- .toUpperCase()
- } catch (e: Exception) {
- text1.text = "ERROR: $e"
- }
- }
- async { // Monad Style
- text2.text = URL("https://ru.wikipedia.org/")
- .readTextAsync() // Асинхронный запрос
- .than { it.getTitle() }
- .than { it.toUpperCase() }
- .onError { "ERROR: $it" }
- }
- }
- private fun String.getTitle() = Regex("<title>(.+?)</title>").find(this)!!.groupValues[1]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement