android 函数返回值,android – 如何从Volley的onResponse函数返回值?
你想使用回调接口,如下所示:public void getString(final VolleyCallback callback) {StringRequest strReq = new StringRequest(Request.Method.GET, url, newResponse.Listener() {@Overridepublic void onResponse(Strin...
你想使用回调接口,如下所示:
public void getString(final VolleyCallback callback) {
StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
result=response;
callback.onSuccess(result);
}
}...
}}
public interface VolleyCallback{
void onSuccess(String result);
}
活动范例代码:
public void onResume(){
super.onResume();
getString(new VolleyCallback(){
@Override
public void onSuccess(String result){
... //do stuff here
}
});
}
您也可以使VolleyCallback更加健壮,如果要进行处理,使用泛型类型,或者添加start(),失败(Exception e),complete()等方法来进行更细粒度的状态检查。
记住这是一个异步调用,所以当你得到结果(inside())时,你将不得不更新视图等。
更多推荐
所有评论(0)