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

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

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐