Update UI thread after network request in Volley Android library

后端 未结 2 588
日久生厌
日久生厌 2021-02-08 07:53

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 sugg

2条回答
  •  终归单人心
    2021-02-08 08:28

    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 successListener = new Listener() {
        @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);
    

提交回复
热议问题