英文:
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<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);
}
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<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?)
}
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
}
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论