【问题标题】:How to connect Android Application to SSL server when you have a CA that isn't trusted by the system?当您拥有不受系统信任的 CA 时,如何将 Android 应用程序连接到 SSL 服务器?
【发布时间】:2018-08-07 05:47:40
【问题描述】:

我正在尝试从受 SSL 保护的网站下载图像。当我在 API 23(Android 6.0) 或更高版本上运行应用程序时,下载图像成功,但当应用程序在较低 API 上运行时,我得到 SSLHandshakeException“java.security.cert。 CertPathValidatorException:找不到证书路径的信任锚。” 消息。

【问题讨论】:

  • 我正在尝试使用 Picasso 下载图片。
  • 尝试在这里分享您的代码

标签: java android ssl ca


【解决方案1】:
// Glide Image loader
compile 'com.github.bumptech.glide:glide:3.7.0'
Use following code inside onBindViewHolder
    int defaultImagePath = R.drawable.default_thumb;
    int errorImagePath = R.drawable.damaged_image;
    holder.mImageViewContactItem.setImageResource(defaultImagePath);

    String uri = photoPath;
    loadImageWithGlide(context, holder.mImageViewContactItem, uri, defaultImagePath,
                            errorImagePath);


public  void loadImageWithGlide(final Context context, ImageView theImageViewToLoadImage,
                                          String theLoadImagePath, int theDefaultImagePath, int tehErrorImagePath) {
        if (context == null) return;

        Glide.with(context) //passing context
                .load(theLoadImagePath) //passing your url to load image.
                .placeholder(theDefaultImagePath) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                .error(tehErrorImagePath)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                //.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                //.fitCenter()//this method help to fit image into center of your ImageView
                .into(theImageViewToLoadImage); //pass imageView reference to appear the image.

        /*  Normal way to Load Image with Glide.
                Glide.with(theContext)
                .load(theImagePath)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(theImageView);*/
    }

【讨论】:

    【解决方案2】:

    您也可以使用 DownloadManager 从 Url 下载图片并保存 到SD卡

    DownloadImages(PictureURL, ImageSavePath);

    public void DownloadImages(String theUrl, String thePath) {
            Uri Download_Uri = Uri.parse(theUrl);
            DownloadManager downloadManager = (DownloadManager) myContext.getSystemService(myContext.DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
    
            //Restrict the types of networks over which this download may proceed.
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
            //Set whether this download may proceed over a roaming connection.
            request.setAllowedOverRoaming(true);
            //Set the local destination for the downloaded file to a path within the application's external files directory
            String[] split = theUrl.split("/");
            //request.setDestinationInExternalFilesDir(myContext, mySdCardImagePath, split[split.length - 1]);
            //request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, split[split.length-1]);
    
            File destinationFile = new File(thePath, split[split.length - 1]);
            Uri uri = null;
    
            // naugat does not need provider here
            uri = Uri.fromFile(destinationFile);
    
            request.setDestinationUri(uri);
    
            //Set the title of this download, to be displayed in notifications (if enabled).
            request.setTitle(split[split.length - 1]);
            //Set a description of this download, to be displayed in notifications (if enabled)
            request.setDescription(thePath);
    
            request.setVisibleInDownloadsUi(true);
    
            //Enqueue a new download and get the reference Id
            long downloadReference = downloadManager.enqueue(request);
            //Check_Image_Status(downloadReference);
        }
    

    【讨论】:

    • 这是一个电子商务应用程序,我想将图像放在回收站视图中。我不打算将它们下载到内存中。
    • 我又上传了一个答案,我已经使用 glide 加载图片的 URL 并且没有遇到任何问题。
    【解决方案3】:

    解决方案触手可及。试试这个链接Security with HTTPS and SSL。这里提供了完整的解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2020-12-01
      • 2016-10-15
      • 1970-01-01
      • 2015-10-17
      • 2012-09-19
      相关资源
      最近更新 更多