如何在 Kotlin 回调中进行此操作?

how to make this in Kotlin callback?

请问我在java中写的这个回调是怎么做的,在kotlin中,代码基本上是基于一个用volley库发出请求的情况,我需要通过接口获取响应,我会很感激


齐射class

...
    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);
    }

主要活动class

. . . 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VolleyRequest volleyRequest = new VolleyRequest(this);
        volleyRequest.stringRequest(getUrlAPI(), new VolleyRequest.VolleyStringResponse() {
            final TextView textView = findViewById(R.id.text);
            @Override
            public void onSuccess(String response) {
                    textView.setText(response);
            }

            @Override
            public void onError(VolleyError error) {
                textView.setText(error.getMessage());
            }
        });
    }
}

齐射class


    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?)
    }

主要活动class

    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
        }
    })