简体   繁体   中英

Volley: Kotlin Class call from JavaCode

I'd like to make simple Volley POST Requests due Java and Kotlin. I'm using both languages in my App so I tried do to my best to use both languages. I came over this tutorial with the following VolleyClass in Kotlin:

class WolfRequest(val url: String, val result: (JSONObject) -> Unit, val error: (String) -> Unit) {

fun POST(vararg params: Pair<String, Any>) {
    // HashMap to pass arguments to Volley
    val hashMap = HashMap<String, String>()
    params.forEach {
        // Convert all Any type to String and add to HashMap
        hashMap[it.first] = it.second.toString()
    }
    // Make the Http Request
    makeRequest(Request.Method.POST, hashMap)
}

private fun makeRequest(method: Int, params: HashMap<String, String>) {
    // Creating a StringRequest
    val req = object : StringRequest(method, url, { res ->

        // Creating JSON object from the response string
        // and passing it to result: (JSONObject) -> Unit function
        result(JSONObject(res.toString().trim()))
    }, { volleyError ->

        // Getting error message and passing it
        // to val error: (String) -> Unit function
        error(volleyError.message!!)
    }) {
        // Overriding getParams() to pass our parameters
        override fun getParams(): MutableMap<String, String> {
            return params
        }
    }

    // Adding request to the queue
    volley.add(req)
}

// For using Volley RequestQueue as a singleton
// call WolfRequest.init(applicationContext) in
// app's Application class
companion object {
    var context: Context? = null
    val volley: RequestQueue by lazy {
        Volley.newRequestQueue(context
                ?: throw NullPointerException(" Initialize WolfRequest in application class"))
    }

    fun init(context: Context) {
        this.context = context
    }
}

}

I'm trying to access this Code from a Java.Class to make a POST Reuqest:

    WolfRequest.Companion.init(getApplicationContext());
    HashMap <String, String> params = new HashMap <> ();
    params.put("id", "123");

    new WolfRequest(config.PING_EVENTS,*new WolfRequest()*
    {

        public void response(JSONObject response) {
            Log.e("Ping","PING");
        }

        public void error(String error) {
            Log.e("Ping",error);
        }
    }).POST(params);

It gives me an error here ( new WolfRequest() ) saying: Cannot inherit from final "...wolfrequest.kt"

I really don't get the error, what's the problem here?

Thank you

Classes in kotlin are final by default. To create a none final class you need to declare it as open . So open class WolfRequest

With new WolfRequest() {} in java you create an anonymous class that extends WolfRequest , so you get that error inheriting from an final class.

To actually call the constructor for WolfRequest you need to pass the three arguments. Something like:

new WolfRequest("", (s) -> Unit.INSTANCE, (s) -> Unit.INSTANCE){
....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM