mk4
mk4

Reputation: 93

How can I read a huge file JSON with kotlin?

I'm trying to read a json file regarding my scheduling lessons at university, getting it from this link : http://diag.uniroma1.it/pannello/?q=export_json

I tried to do it using https://developer.android.com/training/volley/simple which works for very simple json file as in https://helloacm.com/api/factor/?cached&n=10 ( where given a number it returns the factorization of itself).

But applying same reasoning doesn't works. Actually the application that first that previously worked well, now crashes.

 fun factors (x: String){

        val queue = Volley.newRequestQueue(this)
        //val url = "https://helloacm.com/api/factor/?cached&n="+x

        val url = "http://diag.uniroma1.it/pannello/?q=export_json"

        var reply : String = ""

        // Request a string response from the provided URL.
        val stringRequest = StringRequest(
            Request.Method.GET, url,
            Response.Listener<String> { response ->
                // Display the first 500 characters of the response string.
                reply = JSONObject(response.toString()).toString()
                output.text=reply
            },
            Response.ErrorListener { error: VolleyError? ->  output.text = error.toString() })

        // Add the request to the RequestQueue.
        queue.add(stringRequest)

    }

What is the problem? Is the file too big respect the previous one? I'm really new in this stuff

Upvotes: 1

Views: 2281

Answers (1)

Maksym S.
Maksym S.

Reputation: 151

The reason why it does not work is that you are receiving a JSON array but you are trying to parse it as a JSON object.

In order to simplify deserialization, you can check Google's Gson Library. Here's an example of Gson implementation:

  1. Add the following dependency to your build.gradle file and sync your project with Gradle:

    implementation 'com.google.code.gson:gson:2.8.6'
    
  2. Create a model that represents your JSON objects:

    import com.google.gson.annotations.SerializedName
    
    data class DataObject(
        @SerializedName("data")
        val data: String,
        @SerializedName("Descrizione")
        val descrizione: String,
        @SerializedName("Id")
        val id: String,
        @SerializedName("id_aula")
        val idAula: String,
        @SerializedName("minuti_fine")
        val minutiFine: String,
        @SerializedName("minuti_inizio")
        val minutiInizio: String,
        @SerializedName("nome_aula")
        val nomeAula: String,
        @SerializedName("ora_fine")
        val oraFine: String,
        @SerializedName("ora_inizio")
        val oraInizio: String
    )
    
  3. Now you can modify your function to deserialize JSON:

    fun factors(x: String) {
    
        val queue = Volley.newRequestQueue(this)
        //val url = "https://helloacm.com/api/factor/?cached&n="+x
    
        val url = "http://diag.uniroma1.it/pannello/?q=export_json"
    
        var reply: List<DataObject> = listOf()
    
        // Request a string response from the provided URL.
        val stringRequest = StringRequest(
                Request.Method.GET, url,
                Response.Listener<String> { response ->
                    // Display the first 500 characters of the response string.
                    reply = Gson().fromJson(response, object : TypeToken<List<DataObject>>() {}.type)
                    output.text = reply
                },
                Response.ErrorListener { error: VolleyError? -> output.text = error.toString() })
    
        // Add the request to the RequestQueue.
        queue.add(stringRequest)
    }
    

Upvotes: 2

Related Questions