Close Menu
InfovistarInfovistar
  • AI & ML
  • Cybersecurity
  • Startup
  • Tech News
  • Insights
    • Web Development
    • AWS and Cloud
    • Blockchain and Cryptocurrency
    • Chatbots
    • Technology
    • DevOps
    • Resources
  • Courses
    • Machine Learning
      • Python Tutorial
      • TensorFlow Tutorial
      • OpenCV
    • DSA
      • Data Structures
    • Web Development
      • PHP Tutorial
      • CodeIgniter Tutorial
      • CodeIgniter 4 Tutorial
      • CodeIgniter 4 AJAX
      • JavaScript
    • Mobile Development
      • Android Tutorial
  • Tools
    • Beautifier
      • HTML Beautifier
      • JavaScript Beautifier
      • CSS Beautifier
    • Online Compilers
      • Python Compiler
      • Java Compiler
      • JavaScript Editor
      • PHP Compiler
      • C++ Compiler
      • C Compiler
    • Image Optimization
      • Image Compressor
      • JPEG to PNG
      • PNG to JPEG
      • WebP to PNG

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

What's Hot

Ransomware 2.0: How AI Is Changing Cyber Attacks Forever

April 18, 2025

Lovable AI Faces Major Threat from VibeScamming Attacks

April 10, 2025

Top Trends to Include in Your Strategy for Digital Marketing in 2025

April 5, 2025
Facebook X (Twitter) Instagram
Facebook X (Twitter) Instagram Pinterest Vimeo
InfovistarInfovistar
  • AI & ML
  • Cybersecurity
  • Startup
  • Tech News
  • Insights
    • Web Development
    • AWS and Cloud
    • Blockchain and Cryptocurrency
    • Chatbots
    • Technology
    • DevOps
    • Resources
  • Courses
    • Machine Learning
      • Python Tutorial
      • TensorFlow Tutorial
      • OpenCV
    • DSA
      • Data Structures
    • Web Development
      • PHP Tutorial
      • CodeIgniter Tutorial
      • CodeIgniter 4 Tutorial
      • CodeIgniter 4 AJAX
      • JavaScript
    • Mobile Development
      • Android Tutorial
  • Tools
    • Beautifier
      • HTML Beautifier
      • JavaScript Beautifier
      • CSS Beautifier
    • Online Compilers
      • Python Compiler
      • Java Compiler
      • JavaScript Editor
      • PHP Compiler
      • C++ Compiler
      • C Compiler
    • Image Optimization
      • Image Compressor
      • JPEG to PNG
      • PNG to JPEG
      • WebP to PNG
Subscribe
InfovistarInfovistar
Home » Retrofit – SSLHandshakeException Android
Android

Retrofit – SSLHandshakeException Android

InfovistarBy InfovistarMay 2, 2022Updated:January 17, 2025No Comments3 Mins Read
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Retrofit – SSLHandshakeException Android
Share
Facebook Twitter LinkedIn Pinterest Email

Recently, I worked on a remote-networking implementation for an Android eCommerce application, and everything went smoothly. I used the Retrofit networking library and the base URL was http://api.infovistar.in without SSL (an HTTP scheme).

As soon as we finished the MVP, we acquired a domain name and enabled SSL on the server. Consequently, the base URL scheme became HTTPS (example: https://api.infovistar.in).

The change to a secured URL in the app caused chaos in the entire app. All the endpoints were not connecting to the server successfully. There was a problem with the network handshake between the client and server.

HTTP FAILED: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

Solution 1:

In doing some research, I found out the Android system didn’t trust the server certificate. There are several possible reasons for this:

  • The Certificate Authority (CA) that issued the server certificate was unknown.
  • The server certificate wasn’t signed by a CA but was self-signed.
  • The server configuration is missing an intermediate CA.

In my case, this issue occurred because the server certificate was self-signed.

The Android documentation describes a simple way to set it up to trust self-signed certificates, which I will outline in three steps.

Step 1

Add the .crt or .pem file to the raw folder.

The digital certificate will be obtained from the server or you can request it from the backend engineer.

Step 2

Create an XML network security configuration file (network_security_config.xml) as follows:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">api.infovistar.in</domain>
        <trust-anchors>
            <certificates src="@raw/certificate" />
        </trust-anchors>
    </domain-config>
</network-security-config>

Step 3

Provide the application’s network configuration settings in the Manifest.xml file.

Solution 2:

In your Retrofit request, you need to modify your OKHttpClient.Builder object so that your request will create a certificate that can be trusted by your server. Only then can your server allow access to your request.

    public static Retrofit getUnsafeApiClient(Context ctx) {
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(getUnsafeOkHttpClient().build())
                .build();
    }

    public static OkHttpClient.Builder getUnsafeOkHttpClient() {
        try {
            // Create a trust manager that does not validate certificate chains
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @SuppressLint("TrustAllX509TrustManager")
                        @Override
                        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }

                        @SuppressLint("TrustAllX509TrustManager")
                        @Override
                        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[]{};
                        }
                    }
            };

            // Install the all-trusting trust manager
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

            // Create an ssl socket factory with our all-trusting manager
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
            return builder;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

These two solutions should allow you to connect seamlessly with the backend.

Android retrofit SSLHandshakeException
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleIs Blockchain Secure? in Hindi
Next Article Metaverse- the new reality
Infovistar
  • Website
  • Facebook
  • X (Twitter)
  • Instagram
  • LinkedIn

Related Posts

Tech Tips

What Is Google Partner Setup and How Does It Work?

February 28, 2025
Tech News

Google is rolling out Identity Check Feature to Android 15

January 25, 2025
Cybersecurity

Google’s New Restore Credentials Tool Simplifies App Login

November 26, 2024
Add A Comment

Comments are closed.

Blog Categories
  • AI and ML (93)
  • Android (4)
  • AWS and Cloud (7)
  • Blockchain and Cryptocurrency (6)
  • Case Study (7)
  • Chatbots (5)
  • Cybersecurity (71)
  • DevOps (5)
  • Object-Oriented Programming (2)
  • Payment Gateway (4)
  • Resources (5)
  • Search Engine Optimization (3)
  • Startup (34)
  • Tech News (70)
  • Tech Tips (12)
  • Technology (79)
  • Trading (6)
  • Web Development (23)
Top Posts

Google is rolling out Identity Check Feature to Android 15

January 25, 20252,371 Views

How to Integrate Google Gemini to WhatsApp

February 16, 20241,681 Views

OpenAI Unveils Web-Based AI Agent Operator for Task Automation

January 24, 20251,502 Views
Stay In Touch
  • Facebook
  • YouTube
  • WhatsApp
  • Twitter
  • Instagram
  • Pinterest
  • LinkedIn
Latest Articles

Subscribe to Updates

Get the latest tech news from FooBar about tech, design and biz.

Most Popular

Google is rolling out Identity Check Feature to Android 15

January 25, 20252,371 Views

How to Integrate Google Gemini to WhatsApp

February 16, 20241,681 Views

OpenAI Unveils Web-Based AI Agent Operator for Task Automation

January 24, 20251,502 Views
Our Picks

Ransomware 2.0: How AI Is Changing Cyber Attacks Forever

April 18, 2025

Lovable AI Faces Major Threat from VibeScamming Attacks

April 10, 2025

Top Trends to Include in Your Strategy for Digital Marketing in 2025

April 5, 2025

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Contact Us
  • Tools
  • Terms & Conditions
  • Privacy Policy
  • AdSense Disclaimer
© 2025 Infovistar. Designed and Developed by Infovistar.

Type above and press Enter to search. Press Esc to cancel.

Go to mobile version