在凌空的Android库网的请求后,更新UI线程 [英] Update UI thread after network request in Volley Android library
问题描述
我决定给凌空一试,所以presently我必须做了很多REST标注的,所以我通常会创建一个RequestHandler的和ResponseHandler的类,它作为顾名思义处理请求和响应分别。我按照这种模式,这样我就不写了冗余code。我只是通过在动态查询/ URL作为参数,并使用一个开关壳体处理响应于每个请求。但我坚持了以下问题:
I decided to give Volley a try, so presently I have a lot of REST callouts to be done, so I usually create a RequestHandler and a ResponseHandler class which as their names suggest handle requests and responses respectively. I follow this pattern so that I don't write redundant code. I just pass in the dynamic query/url as parameter and using a switch case handle the response to each of the requests. But I am stuck with the below problem :
我也没办法从我所说的RequestHandler的类更新我的UI线程。我曾尝试或已经知道至今:
I have no way of updating my UI thread from where I call the RequestHandler class. What I have tried or already know so far :
- 请用户界面元素(TextView的,列表视图)静态和更新他们的反应来后。
- 传入的上下文参数并且接收到的响应之后更新UI。
- 写请求和响应,内部类的活动中
- 在摆脱抽射。
我在想,怎么你们这样做呢?是否有任何模式不是请求/响应处理器模式我跟着好?更新千篇一律的UI线程什么办法?
I was wondering, how you guys do it? Is there any pattern better than the Request/Response Handler pattern I follow? Any way of updating the UI thread following the same pattern?
在此先感谢!
推荐答案
我用凌空抽射,这是我做的。在code进去你的活动的任何地方。
I use volley, and this is what I do. The code goes anywhere in your activity.
import com.android.volley.Response.Listener;
import static com.android.volley.Response.ErrorListener;
Listener<YOURDATACLASS> successListener = new Listener<YOURDATACLASS>() {
@Override
public void onResponse(YOURDATACLASS data) {
// Check to make sure that the activity hasn't been destroyed while the call was in flight.
if (! isFinishing()) {
//DO YOUR UI UPDATE, such as
TextView textview = (TextView) findViewById(R.id.yourtextview);
textview.setText("blah blah blah");
}
}
};
ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//DO SOMETHING ON FAILURE
}
YOURAPICALL(successListener, failurelistener);
这篇关于在凌空的Android库网的请求后,更新UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!