【问题标题】:How to override in Kotiln Volley JsonObjectRequest class?如何在 Kotlin Volley JsonObjectRequest 类中覆盖?
【发布时间】:2020-02-12 06:44:24
【问题描述】:

我正在编写一个有类的 Kotlin 应用程序。我需要那个类来扩展 JsonObjectRequest,因为我需要重写函数

override fun parseNetworkResponse(response: NetworkResponse?): Response<T>

那是因为我需要在 Kotlin 中解释服务器发送的 HTTP 响应代码。

但是,我承认我是 Kotlin 的新手,还没有弄清楚如何扩展 JsonObjectRequest 类。我一直遇到愚蠢的编译器问题。

有人可以提供一个简单的例子吗?

【问题讨论】:

    标签: kotlin android-volley


    【解决方案1】:

    经过一番反复,我终于弄明白了。在这里发布它,因为它可能对其他人有用 -

    class DataRequest(
        method: Int,
        uri: String,
        jsonObject: JSONObject,
        listener: Response.Listener<JSONObject>,
        errorListener: Response.ErrorListener
    ) :
        JsonObjectRequest(method, uri, jsonObject, listener, errorListener)
    {
        override fun parseNetworkResponse(response: NetworkResponse): Response<JSONObject>
        {
            try
            {
                val jsonString = String(
                    response.data,
                    Charset.forName(HttpHeaderParser.parseCharset(response.headers))
                )
                return Response.success(
                    JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)
                )
            } catch (e: UnsupportedEncodingException)
            {
                return Response.error(ParseError(e))
            } catch (je: JSONException)
            {
                return Response.error(ParseError(je))
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2013-05-22
      相关资源
      最近更新 更多