【问题标题】:How can I read a huge file JSON with kotlin?如何使用 kotlin 读取大文件 JSON?
【发布时间】:2019-10-28 18:48:34
【问题描述】:

我正在尝试阅读有关我在大学安排课程的 json 文件,从以下链接获取它:http://diag.uniroma1.it/pannello/?q=export_json

我尝试使用 https://developer.android.com/training/volley/simple 来实现它,它适用于非常简单的 json 文件,如 https://helloacm.com/api/factor/?cached&n=10 (给定一个数字,它返回自身的因式分解)。

但是应用相同的推理是行不通的。实际上,最初运行良好的应用程序现在崩溃了。

 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)

    }

有什么问题?文件太大了尊重前一个吗?我对这些东西真的很陌生

【问题讨论】:

  • 你能分享崩溃日志吗?如果它崩溃了,我认为这与文件大小无关。可能还有其他一些 RuntimeException。
  • @Rachit 我在我的智能手机上运行它,当我按下按钮获得响应时,应用程序停止并出现:“application_name has stop Open app again”
  • 您可以通过在手机上运行应用程序并转到 Android Studio 上的 Logcat 选项卡来检查崩溃日志。当崩溃发生时,您会看到崩溃的原因。

标签: android kotlin android-volley


【解决方案1】:

它不起作用的原因是你正在接收一个 JSON 数组,但你试图将它解析为一个 JSON 对象。

为了简化反序列化,可以查看Google's Gson Library。 下面是一个 Gson 实现的例子:

  1. 将以下依赖项添加到您的 build.gradle 文件中并将您的项目与 Gradle 同步:

    implementation 'com.google.code.gson:gson:2.8.6'
    
  2. 创建一个代表您的 JSON 对象的模型:

    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. 现在您可以修改函数以反序列化 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)
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 2018-08-14
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2021-12-21
    相关资源
    最近更新 更多