android:通过HTTPS从API获取图像

时间:2016-07-04 09:56:27

标签: android image https

我的API请求: _HTTPS:// myserver / getcaptchaimage?accesstoken = mytoken& userid = 123

回复:验证码图片

标题:

缓存控制私有 内容类型 image / png 日期星期一,04 Jul 2016 09:15:05 GMT 服务器Microsoft-IIS / 7.5 Transfer-Encoding chunked X-AspNet-Version 4.0.30319 X-Powered-By ASP.NET

请告诉我如何在Android中获取对ImageView的响应和加载。

我的代码(https.connect()时的例外)

private void getCaptchaImageFromServer() {
        String URL = ServerPath.SERVER_PATH_GET_CAPTCHA_IMAGE + "?accesstoken=" + UserInforLogin.getInstance(this).getToken()
                + "&userid=" + UserInforLogin.getInstance(this).getUserId();
        LogUtils.e("test-request captcha image", URL);

        try {
            URL imageURL = new URL(URL);
            ImageView imageView = (ImageView) findViewById(R.id.imageviewTest);

            HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[] {new NullX509TrustManager()}, new SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

            HttpsURLConnection https = (HttpsURLConnection) imageURL.openConnection();
            https.connect();
            InputStream input = https.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            imageView.setImageBitmap(myBitmap);
        } catch (Exception e) {
            // TODO: handle exception
            LogUtils.e("test-request captcha image ERROR", "ERROR");
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以使用其他第三方库作为图像

  1. Universal Image Loader

  2. Fresco