Volley implements verification code function

    In the company's project, in order to prevent the machine from registering, it is necessary to add the verification code function. At first, I thought it was just a picture, just load an address through glide, but the colleague of the interface said that the verification code interface returns a stream, because the server should not do verification. The storage of code pictures, because the verification is constantly changing, but the company's interfaces are all https, there are corresponding certificate verification links and header information verification, and the existing network requests in the project are encapsulated volley json requests, only The interface that should return json, I also learned that volley has jsonRequest, StringReque.

     I began to plan to encapsulate a set of network requests for the verification code interface. An accidental idea, does volley provide a corresponding api? Through research, it is found that volley provides ImageRequest, and the implementation method is as follows:

    

MyImageRequest imageRequest = new MyImageRequest(
                NetworkHelper.processUrl(NetConfig.imgVerificationCode),
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        try {
                            isGettingImageCode = false;

                            img_verificationCode_btn.setImageBitmap(response);
                            img_verificationCode_btn.setVisibility(View.VISIBLE);
                            tv_get_imgCode_error.setVisibility(View.GONE);
                            pb_loading_imgcode.setVisibility(View.GONE);
                        } catch (Exception e) {
                            e.printStackTrace ();
                        }
                    }
                }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isGettingImageCode = false;
                img_verificationCode_btn.setVisibility(View.GONE);
                tv_get_imgCode_error.setVisibility(View.VISIBLE);
                pb_loading_imgcode.setVisibility(View.GONE);
                tv_get_imgCode_error.setText("Click to retry");
// ToastUtil.show("The network is abnormal, the image verification code acquisition failed");
            }

        });
        MyApplication.getInstance().getRequestQueuequeue().add(imageRequest);

 

 

public class MyImageRequest extends ImageRequest {
    public MyImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight, ImageView.ScaleType scaleType, Bitmap.Config decodeConfig, Response.ErrorListener errorListener) {
        super(url, listener, maxWidth, maxHeight, scaleType, decodeConfig, errorListener);
    }

    public MyImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight, Bitmap.Config decodeConfig, Response.ErrorListener errorListener) {
        super(url, listener, maxWidth, maxHeight, decodeConfig, errorListener);

    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return initHeader(urlBuilder(getUrl(),null),null,"GET"); //Encapsulate head information

    }

The related certificate verification is mentioned in the previous article rewriting

in HTTPS TrustManager
checkServerTrusted

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479424&siteId=291194637