Update UI thread after network request in Volley Android library

后端 未结 2 592
日久生厌
日久生厌 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:14

    This works for me.

    Map params = new HashMap<>();
        params.put("dep", DEP);
    
        CustomPostRequest request = new CustomPostRequest(Request.Method.POST, Uris.URL_GET_DEP_CAT, params,
                new Response.Listener() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
    
                        List valores = parsear_y_devolver_valores(jsonObject);
    
                        gestionar_entregas(valores);
    
                        //aqui quitar los dialogos
    
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
    
            }
        });
    
        ApiController.getInstance().addToRequestQueue(request);
    

    ApiController is my Aplication singleton class.

    public class ApiController extends Application {
    
    public static final String TAG = ApiController.class.getSimpleName();
    
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    
    private static ApiController mInstance;
    
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        /*FacebookSdk.sdkInitialize(getApplicationContext());
        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.example.android.facebookloginsample",  // replace with your unique package name
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException e) {
    
        } catch (NoSuchAlgorithmException e) {
    
        }*/
    
    }
    
    public static synchronized ApiController getInstance() {
        return mInstance;
    }
    
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }
    
    public  void addToRequestQueue(Request req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }
    
    public  void addToRequestQueue(Request req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }
    
    public void cancelPendingRequests(Object tag) {
    
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    
    }
    

    }

提交回复
热议问题