如何在一个有返回值的方法中进行网络操作,然后返回网络操作后的值?
比如
private String A(){
StringRequest stringRequest=new StringRequest(Request.Method.POST, MyApplication.HOST + "/mobileLogin.do",
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
sout("获取的"+s)
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<>();
map.put("msgno","001010");
map.put("uid",s);
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
我想返回这个获取的结果,但是volley的操作是在异步,根本没法return,那这个怎么办
You can use interface callback
Declare an interface first
inside the methodThen use
Use RxJava or EventBus
Then do it in the asynchronous callback function. Or pass in a method or function where the asynchronous return occurs.
You can change your mind and don’t need a
volley
中返回结果。当请求成功后对结果进行封装,例如直接使用List
或者结合类进行存储,然后可以通过Handler
mechanism to obtain data and operations from it.I need a user information provider when using Rongyun. According to the stringId passed by his method, I then use this id to obtain the information corresponding to this id from my server,,, so there is no way to operate volley first,
RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
You can use Handler directly
I re-read your question, it seems to involve multi-layer callbacks. I think RxAndroid is more suitable here
I have just started using it, and the code I write is not very pretty. . . Asking for guidance from experts