如何在Kotlin中创建回调?

huangapple go评论72阅读模式
英文:

how to make this in Kotlin callback?

问题

下面是您提供的Java代码在Kotlin中的翻译:

// Volley class

fun stringRequest(url: String, volleyResponse: VolleyStringResponse) {
    val stringRequest = StringRequest(Request.Method.GET, url,
        Response.Listener { response ->
            volleyResponse.onSuccess(response)
        },
        Response.ErrorListener { error ->
            volleyResponse.onError(error)
        }
    )
    VolleySingleton.getInstance(context).addToRequestQueue(stringRequest)
}

// Interface

interface VolleyStringResponse {
    fun onSuccess(response: String)
    fun onError(error: VolleyError)
}

// MainActivity class

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val volleyRequest = VolleyRequest(this)
        volleyRequest.stringRequest(getUrlAPI(), object : VolleyRequest.VolleyStringResponse {
            val textView = findViewById<TextView>(R.id.text)
            override fun onSuccess(response: String) {
                textView.text = response
            }

            override fun onError(error: VolleyError) {
                textView.text = error.message
            }
        })
    }
}

请注意,以上内容只是您提供的代码的翻译版本,不包含其他内容。

英文:

could you how I do this callback written in java, in kotlin, the code is basically based on a case that makes a request with the volley library and I need to obtain the response through the interface, I would appreciate it

<br>Volley class<br/>

...
    public void stringRequest(String url,final VolleyStringResponse volleyResponse){
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener&lt;String&gt;() {
            @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);
    }

MainActivity 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());
            }
        });
    }
}

答案1

得分: 2

Volley 类

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 VolleyStringResponse {
    fun onSuccess(response: String?)
    fun onError(error: VolleyError?)
}

MainActivity 类

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
    }
})
英文:

Volley class


    fun stringRequest(url: String?, volleyResponse: VolleyStringResponse) {
        val stringRequest = StringRequest(Request.Method.GET, url, object: Listener&lt;String?&gt;() {
            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?)
    }

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

huangapple
  • 本文由 发表于 2020年10月7日 09:52:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64236099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定