你想使用回调接口,如下所示:

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())时,你将不得不更新视图等。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐