[英]Volley: Kotlin Class call from JavaCode
I'd like to make simple Volley POST Requests due Java and Kotlin. 我想根据Java和Kotlin提出简单的Volley POST请求。 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:
我在Kotlin中使用以下VolleyClass来学习本教程:
class WolfRequest(val url: String, val result: (JSONObject) -> Unit, val error: (String) -> Unit) { class WolfRequest(val url:字符串,val结果:(JSONObject)-> Unit,val错误:(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: 我正在尝试从Java.Class访问此代码以进行POST请求:
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" 它给我一个错误( new WolfRequest() ),它说:无法从最终的“ ... wolfrequest.kt”继承
I really don't get the error, what's the problem here? 我确实没有收到错误,这是什么问题?
Thank you 谢谢
Classes in kotlin are final by default. 默认情况下,kotlin中的类是最终的。 To create a none final class you need to declare it as
open
. 要创建一个无最终类,您需要将其声明为
open
。 So open class WolfRequest
所以
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. 使用Java中的
new WolfRequest() {}
,您可以创建一个扩展WolfRequest
的匿名类,因此您将从继承的类继承该错误。
To actually call the constructor for WolfRequest you need to pass the three arguments. 要实际为WolfRequest调用构造函数,您需要传递三个参数。 Something like:
就像是:
new WolfRequest("", (s) -> Unit.INSTANCE, (s) -> Unit.INSTANCE){
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.