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. The principal I use for login, the second for send a get http and receive a json object.
Where is my problem? I wrong to use 2 different activity o the problem is 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 } }