0

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 } 

}

2 Answers 2

1

Are your icons / resources appropriately sized for a mobile application? If your assets are too large, inflation could take over the main thread.

Sign up to request clarification or add additional context in comments.

Comments

0

You'd better to use Retrofit + Rxjava for handling Network call.

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

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

You can also refer this link for check example: How to get the response URL from Retrofit?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.