To get a picture from the server, I need to attach headers, request method (POST), request body ( JSONObject with several key-value fields) and certificate.

Everything works well in manual installation of images in ImageView , but now you need to use the Glide library, in which it is not known how to add all this data.

This is how my request to the server looks like, in which Request is a container class that stores the headers, HttpMethod and request body ( JSONObject ):

 private String executeRequest(Request request) { return executeRequest(buildHttpRequest(request)); } private String executeRequest(HttpRequestBase httpRequest) { String response = ""; String url = httpRequest.getURI().toString(); boolean useCerificates = url.contains(HOST_SUBSTRING); try{ URL URLObject = new URL(url); HttpURLConnection connection; connection = (HttpURLConnection) URLObject.openConnection(); if(connection instanceof HttpsURLConnection) { if (useCerificates) ((HttpsURLConnection)connection) .setSSLSocketFactory(getSSLContext().getSocketFactory()); else { SSLContext sslContext; sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, null, new java.security.SecureRandom()); ((HttpsURLConnection)connection) .setSSLSocketFactory(sslContext.getSocketFactory()); } } connection.setRequestMethod(httpRequest.getMethod()); Header[] requestHeaders = httpRequest.getAllHeaders(); for (Header requestHeader : requestHeaders) connection.setRequestProperty(requestHeader.getName(), requestHeader.getValue()); if (connection.getRequestMethod().equals("POST")) { String requestBody = getRequestBody(httpRequest); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream()); outputStreamWriter.write(requestBody); outputStreamWriter.flush(); outputStreamWriter.close(); } int statusCode = connection.getResponseCode(); if (statusCode == HttpURLConnection.HTTP_OK) response = getResponseString(connection.getInputStream(),"utf-8"); else{ response = getResponseString(connection.getErrorStream(),"utf-8"); } connection.disconnect(); }catch (Exception e){ e.printStackTrace(); } return response; } 

    1 answer 1

    In Glide , you can add headers to your request using GlideUrl :

     GlideUrl glideUrl = new GlideUrl("url", new LazyHeaders.Builder() .addHeader("key1", "value1") .addHeader("key1", "value2") .build()); Glide.with(context) .load(glideUrl) .into(targetImageView); 

    Installing an SSL certificate is not so easy, but in general it is possible using OkHttp . More details can be found in the article:

    Glide Module Example: Accepting Self-Signed HTTPS Certificates