How can I use the Google Volley network library to initiate HTTP requests with headers ?, Volleyheader
By Long Luo
Since the data source update speed of the partner's third-party iQiyi video is very slow, it provides a new API through feedback with iQiyi.
By reading the new API description, when initiating an HTTP Get request, ** two encrypted Header parameters must be included at the same time **, they are the key, timestamp, and client parameters after the timestamp and MD5 encryption. Otherwise, the correct request cannot be returned.
Currently, the Android client uses the Google open-source Volley library, which supports various HTTP requests, image caching, and JSON parsing, providing powerful performance. Previously Used interfaces directly initiate HTTP Get requests with relevant parameters.
By reading Volley related information, you can find the following methods to include the Header parameter when initiating an HTTP request. The Code is as follows:
public void makeHTTPrequest(String url) { MyLog.d(TAG, "makeHTTPrequest, url=" + url); JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { MyLog.d(TAG, "response=" + response); parseiQiyiInterfaceImageResponse(response); } catch (Exception e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (error instanceof NetworkError) { } else if (error instanceof ClientError) { } else if (error instanceof ServerError) { } else if (error instanceof AuthFailureError) { } else if (error instanceof ParseError) { } else if (error instanceof NoConnectionError) { } else if (error instanceof TimeoutError) { } MyLog.e(TAG, "onErrorResponse, error=" + error); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("t", iQiyiInterface.getEncryptTimestamp()); headers.put("sign", iQiyiInterface.getSign()); // MyLog.d(TAG, "headers=" + headers); return headers; } }; // Set a retry policy in case of SocketTimeout & ConnectionTimeout // Exceptions. Volley does retry for you if you have specified the // policy. jsonObjRequest.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); jsonObjRequest.setTag(TAG); jsonObjRequest.setShouldCache(true); mQueue.add(jsonObjRequest); mQueue.start(); }
The Header parameters are stored in HashMap.
After obtaining the JSON data, the rest is to parse the data, and we will not go into details here.
During this process, we also encountered many small incidents. For example, the sign value of the Header does not support uppercase letters, and the results are also in vain. After the apk is written to the mobile phone, you also need to use tcpdump to capture data packets and check whether the correct data is returned.
Finally, we found a Smart Header plug-in Chrome, which perfectly solved the above problem. It does not need to capture packets to verify the returned results every time. It can be directly in the Chrome browser, saving a lot of time.
In short, I hope this article will help you. Thank you!
Created by Long Luo at 12:30:03 @ Shenzhen, China.