Java 如何在Kotlin回调中进行此操作?
你知道我是如何用java编写这个回调的吗,在kotlin中,代码基本上是基于一个案例,该案例通过volley库发出请求,我需要通过接口获得响应,我将不胜感激Java 如何在Kotlin回调中进行此操作?,java,android,android-studio,kotlin,android-volley,Java,Android,Android Studio,Kotlin,Android Volley,你知道我是如何用java编写这个回调的吗,在kotlin中,代码基本上是基于一个案例,该案例通过volley库发出请求,我需要通过接口获得响应,我将不胜感激 截击类 ... public void stringRequest(String url,final VolleyStringResponse volleyResponse){ StringRequest stringRequest = new StringRequest(Request.Method.GET, ur
截击类
...
public void stringRequest(String url,final VolleyStringResponse volleyResponse){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
volleyResponse.onSuccess(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
volleyResponse.onError(error);
}
});
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
}
//Interface
public interface VolleyStringResponse{
void onSuccess(String response);
void onError(VolleyError error);
}
截击班
fun stringRequest(url: String?, volleyResponse: VolleyStringResponse) {
val stringRequest = StringRequest(Request.Method.GET, url, object: Listener<String?>() {
fun onResponse(response: String?) {
volleyResponse.onSuccess(response)
}
}, object: ErrorListener() {
fun onErrorResponse(error: VolleyError?) {
volleyResponse.onError(error)
}
})
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest)
}
//Interface
interface VolleyStringResponse {
fun onSuccess(response: String?)
fun onError(error: VolleyError?)
}
这回答了你的问题吗?当然,我不知道android studio有这个工具,非常感谢!
fun stringRequest(url: String?, volleyResponse: VolleyStringResponse) {
val stringRequest = StringRequest(Request.Method.GET, url, object: Listener<String?>() {
fun onResponse(response: String?) {
volleyResponse.onSuccess(response)
}
}, object: ErrorListener() {
fun onErrorResponse(error: VolleyError?) {
volleyResponse.onError(error)
}
})
VolleySingleton.getInstance(context).addToRequestQueue(stringRequest)
}
//Interface
interface VolleyStringResponse {
fun onSuccess(response: String?)
fun onError(error: VolleyError?)
}
val volleyRequest = VolleyRequest(this)
volleyRequest.stringRequest(getUrlAPI(), object: VolleyStringResponse() {
val textView: TextView = findViewById(R.id.text)
override fun onSuccess(response: String?) {
textView.text = response
}
fun onError(error: VolleyError) {
textView.text = error.message
}
})