0

I'm struggling with asynchronous calls - the app is just crashing. I want to load a JSON-file (containing 100 JSON-objects) from an URL and then send it to RecyclerView.

Here is the MainActivity-class:

class MainActivity : AppCompatActivity() {

    lateinit var recyclerView: RecyclerView
    lateinit var linearLayoutManager: LinearLayoutManager

    private val url = [//some address here]

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

        recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = linearLayoutManager

        AsyncTaskHandler().execute(url)

    }

    inner class AsyncTaskHandler : AsyncTask<String, String, String>() {
        override fun onPreExecute() {
            super.onPreExecute()
        }

        override fun doInBackground(vararg url: String?): String {
            val text: String
            val connection = URL(url[0]).openConnection() as HttpURLConnection
            try {
                connection.connect()
                text = connection.inputStream.use { it.reader().use {reader -> reader.readText()} }
            } finally {
                connection.disconnect()
            }
            return text
        }

        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
            handleJson(result)
        }
    }

    private fun handleJson(jsonString: String?) {
        val jsonArray = JSONArray(jsonString)
        var list = mutableListOf<DataSet>()

        var i = 0
        while (i < jsonArray.length()) {
            val jsonObject = jsonArray.getJSONObject(i)

            list.add(DataSet(
                jsonObject.getString("title"),
                jsonObject.getString("type")
            ))
            i++
        }

        val adapter = Adapter(list)
        recyclerView.adapter = adapter
    }
}

...and ListAdapter-class:

class Adapter (private var targetData: MutableList<DataSet>): RecyclerView.Adapter<ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.element, parent, false)
        return ViewHolder(v);
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = targetData[position]

        holder.title?.text = item.title
        holder.type?.text = item.type
    }

    override fun getItemCount(): Int {
        return targetData.size
    }
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var title = itemView.findViewById<TextView>(R.id.itemTitle)
    var type = itemView.findViewById<TextView>(R.id.itemType)
}


What might be the problem here? Is there any better option to perform this?

Corei7
  • 43
  • 2
  • It's very difficult to debug a crash without a stack trace. Please see [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) for advice on what to do once you have the stack trace. If you still need help, please edit your question to include the stack trace. – Ryan M Feb 24 '20 at 23:36
  • I am using Anko for asynchronous calls. That library is very usefull. You should just use code like that; doAsync { .. .. onComplete{ .. .. } } – Murat Çakır Feb 25 '20 at 06:44
  • I send request in doAsycn method, then i set adapter in onComplete method. – Murat Çakır Feb 25 '20 at 06:57

0 Answers0