简体   繁体   English

Kotlin主线程

[英]Kotlin MainThread

I'm new in Kotlin. 我是科特林的新手。 I have a problem, with "The application may be doing too much work on its main thread". 我有一个问题,“应用程序可能在其主线程上做太多工作”。

I have 2 different activity. 我有2种不同的活动。 The principal I use for login, the second for send a get http and receive a json object. 我用于登录的主体,第二个用于发送get http和接收json对象。

Where is my problem? 我的问题在哪里? I wrong to use 2 different activity o the problem is asyncdo? 我错误地使用2种不同的活动o问题是asyncdo? I am in deep sea. 我在深海。

class GitActivity: AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.git_activity)

    Recycle_view.layoutManager = LinearLayoutManager(this)
    Recycle_view.adapter = MainAdapter()


    var url = intent.getStringExtra("URL")

    doAsync {

        fetchJson(url)
        uiThread { longToast("Request performed") }
    }

}

fun fetchJson(url: String) : List<NameFileList> {

    var request = Request.Builder().url(url).build()
    val client = OkHttpClient()
    client.newCall(request).enqueue(object : Callback {


        override fun onResponse(call: Call, response: Response) {
            print("sono qui")
            print(url)
            val body = response?.body()?.string()
            print(body)
        }

        override fun onFailure(call: Call, e: IOException) {
            print("Error Failure")
        }


    })

} }

and my Main Class 和我的主要班级

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    SearchButton.setOnClickListener {

            verifyNetwork()

    }
}

private fun getUsername(): String {

    val username = Username_editText.text.toString()
    Log.d("MainActivity", "UserName is + $username")
    return username

}

private fun getRepositoryName(): String {

    val nameRepository = RepositoryName_editText.text.toString()
    Log.d("MainActivity", "UserName is + $nameRepository")
    return nameRepository

}

private fun verifyNetwork()
{
    if(isConnected(this)){

        val url = CreateUrl().createUrl(getUsername(), getRepositoryName())
        print(url)
        var intent = Intent(this, GitActivity::class.java)
        intent.putExtra("URL", url)
        startActivity(intent)
    }

    else {

        //POPUP DI ERRORE NETWORK
    }


}

private fun isConnected(context: Context): Boolean {
    val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork = cm.activeNetworkInfo
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting
}

} }

Are your icons / resources appropriately sized for a mobile application? 您的图标/资源是否适合移动应用程序的大小? If your assets are too large, inflation could take over the main thread. 如果您的资产太大,通货膨胀可能会占据主导地位。

You'd better to use Retrofit + Rxjava for handling Network call. 您最好使用Retrofit + Rxjava来处理网络呼叫。

Retrofit : 改造:
https://square.github.io/retrofit/ https://square.github.io/retrofit/

RxJava : RxJava:
https://github.com/ReactiveX/RxJava https://github.com/ReactiveX/RxJava

You can also refer this link for check example: How to get the response URL from Retrofit? 您还可以参考以下链接获取检查示例: 如何从Retrofit获取响应URL?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM