如何在一个有返回值的方法中进行网络操作,然后返回网络操作后的值?
比如
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,那这个怎么办
黄舟2017-04-18 09:42:00
You can use interface callback
Declare an interface first
public interface RequestCallback{
void onSuccess(String data);
void onFail(String error);
}
Then use
private String A(RequestCallback callback){
StringRequest stringRequest=new StringRequest(Request.Method.POST, MyApplication.HOST + "/mobileLogin.do",
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
sout("获取的"+s)
if(callback!=null){
callback.onSuccess(s);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
if(callback!=null){
callback.onFail(volleyError.getMessage);
}
}
}){
@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);
}
怪我咯2017-04-18 09:42:00
Then do it in the asynchronous callback function. Or pass in a method or function where the asynchronous return occurs.
巴扎黑2017-04-18 09:42:00
You can change your mind and don’t need a volley
中返回结果。当请求成功后对结果进行封装,例如直接使用List
或者结合类进行存储,然后可以通过Handler
mechanism to obtain data and operations from it.
ringa_lee2017-04-18 09:42:00
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() {
@Override
public UserInfo getUserInfo(final String s) {
volley操作,需要使用到s
PHP中文网2017-04-18 09:42:00
You can use Handler directly
I re-read your question, it seems to involve multi-layer callbacks. I think RxAndroid is more suitable here
//假设已经有两个方法,getStringID()用于获取你的用户id
//getUserInfo(String s)根据传入的StringID从你的服务器上获取用户信息
//UserInfo是用户信息类
Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext(getStringID());//获取id
}
})
.map(new Func1<String, String>() {
@Override
public String call(String s) {
//如果你要处理得到的StringID的话,写在这里,否则不用写
return s;
}
})
.flatMap(new Func1<String, Observable<UserInfo>>() {
@Override
public Observable<UserInfo> call(String s) {
//这个s就是上面return的
//返回一个包裹着UserInfo的Observable
return Observable.just(getUserInfo(s));
}
})
.subscribeOn(Schedulers.io())//这段代码上面的都会在子线程中工作
.observeOn(AndroidSchedulers.mainThread())//这段代码下面的会在主线程中工作
.subscribe(new Action1<UserInfo>() {
@Override
public void call(UserInfo userInfo) {
//你想对UserInfo进行的操作,比如显示个用户名啥的
}
});
I have just started using it, and the code I write is not very pretty. . . Asking for guidance from experts