HTTP FAILED: java.net.SocketTimeoutException: timeout on new Android gradle











up vote
1
down vote

favorite












I'm using



 classpath 'com.android.tools.build:gradle:3.2.1'

distributionUrl=https://services.gradle.org/distributions/gradle-4.10.2-all.zip


and every time I try to connect to my server API it times out.




HTTP FAILED: java.net.SocketTimeoutException




My server API is perfectly fine and I've tested it with Postman & Direct URL. The only way to fix this is to downgrade to 3.0.4 gradle plugin or connect android device to VPN. I'm from IRAN I doubt that if there is any access issues to my servers from my country. I even tried setting long timeouts for my HTTPClient but I still get the same error on every single API. However after 20-30 retries I can finally receive only 1 successful response.



I'm using Retrofit2.



I also tried setting this in my manifest but nothing changed



android:usesCleartextTraffic="true"


Anyone else struggling with this problem? Anyone have a solution?










share|improve this question






















  • Mohmmad hossein for using android studio you need use VPN and nothing.
    – amir133
    Nov 14 at 19:04












  • @amir133 there is no problem with building or installing the application. the problem is runtime, I can't connect to my server apis
    – Mohammad Hosein Kalantarian
    Nov 15 at 7:07















up vote
1
down vote

favorite












I'm using



 classpath 'com.android.tools.build:gradle:3.2.1'

distributionUrl=https://services.gradle.org/distributions/gradle-4.10.2-all.zip


and every time I try to connect to my server API it times out.




HTTP FAILED: java.net.SocketTimeoutException




My server API is perfectly fine and I've tested it with Postman & Direct URL. The only way to fix this is to downgrade to 3.0.4 gradle plugin or connect android device to VPN. I'm from IRAN I doubt that if there is any access issues to my servers from my country. I even tried setting long timeouts for my HTTPClient but I still get the same error on every single API. However after 20-30 retries I can finally receive only 1 successful response.



I'm using Retrofit2.



I also tried setting this in my manifest but nothing changed



android:usesCleartextTraffic="true"


Anyone else struggling with this problem? Anyone have a solution?










share|improve this question






















  • Mohmmad hossein for using android studio you need use VPN and nothing.
    – amir133
    Nov 14 at 19:04












  • @amir133 there is no problem with building or installing the application. the problem is runtime, I can't connect to my server apis
    – Mohammad Hosein Kalantarian
    Nov 15 at 7:07













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm using



 classpath 'com.android.tools.build:gradle:3.2.1'

distributionUrl=https://services.gradle.org/distributions/gradle-4.10.2-all.zip


and every time I try to connect to my server API it times out.




HTTP FAILED: java.net.SocketTimeoutException




My server API is perfectly fine and I've tested it with Postman & Direct URL. The only way to fix this is to downgrade to 3.0.4 gradle plugin or connect android device to VPN. I'm from IRAN I doubt that if there is any access issues to my servers from my country. I even tried setting long timeouts for my HTTPClient but I still get the same error on every single API. However after 20-30 retries I can finally receive only 1 successful response.



I'm using Retrofit2.



I also tried setting this in my manifest but nothing changed



android:usesCleartextTraffic="true"


Anyone else struggling with this problem? Anyone have a solution?










share|improve this question













I'm using



 classpath 'com.android.tools.build:gradle:3.2.1'

distributionUrl=https://services.gradle.org/distributions/gradle-4.10.2-all.zip


and every time I try to connect to my server API it times out.




HTTP FAILED: java.net.SocketTimeoutException




My server API is perfectly fine and I've tested it with Postman & Direct URL. The only way to fix this is to downgrade to 3.0.4 gradle plugin or connect android device to VPN. I'm from IRAN I doubt that if there is any access issues to my servers from my country. I even tried setting long timeouts for my HTTPClient but I still get the same error on every single API. However after 20-30 retries I can finally receive only 1 successful response.



I'm using Retrofit2.



I also tried setting this in my manifest but nothing changed



android:usesCleartextTraffic="true"


Anyone else struggling with this problem? Anyone have a solution?







java android http timeout retrofit2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 at 18:54









Mohammad Hosein Kalantarian

62




62












  • Mohmmad hossein for using android studio you need use VPN and nothing.
    – amir133
    Nov 14 at 19:04












  • @amir133 there is no problem with building or installing the application. the problem is runtime, I can't connect to my server apis
    – Mohammad Hosein Kalantarian
    Nov 15 at 7:07


















  • Mohmmad hossein for using android studio you need use VPN and nothing.
    – amir133
    Nov 14 at 19:04












  • @amir133 there is no problem with building or installing the application. the problem is runtime, I can't connect to my server apis
    – Mohammad Hosein Kalantarian
    Nov 15 at 7:07
















Mohmmad hossein for using android studio you need use VPN and nothing.
– amir133
Nov 14 at 19:04






Mohmmad hossein for using android studio you need use VPN and nothing.
– amir133
Nov 14 at 19:04














@amir133 there is no problem with building or installing the application. the problem is runtime, I can't connect to my server apis
– Mohammad Hosein Kalantarian
Nov 15 at 7:07




@amir133 there is no problem with building or installing the application. the problem is runtime, I can't connect to my server apis
– Mohammad Hosein Kalantarian
Nov 15 at 7:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I found the problem! It was a problem with self-signed SSL servers. Here's the fix:
you have to create an UnsafeClient class and then set it to retrofit's client.



public class UnsafeOkHttpClient {  
public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager trustAllCerts = new TrustManager {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
}

@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]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});

OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("BASEURL")
.client(okHttpClient);





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306992%2fhttp-failed-java-net-sockettimeoutexception-timeout-on-new-android-gradle%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I found the problem! It was a problem with self-signed SSL servers. Here's the fix:
    you have to create an UnsafeClient class and then set it to retrofit's client.



    public class UnsafeOkHttpClient {  
    public static OkHttpClient getUnsafeOkHttpClient() {
    try {
    // Create a trust manager that does not validate certificate chains
    final TrustManager trustAllCerts = new TrustManager {
    new X509TrustManager() {
    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
    }

    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
    }

    @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]);
    builder.hostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
    return true;
    }
    });

    OkHttpClient okHttpClient = builder.build();
    return okHttpClient;
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    }
    }

    OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

    Retrofit.Builder builder = new Retrofit.Builder()
    .baseUrl("BASEURL")
    .client(okHttpClient);





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      I found the problem! It was a problem with self-signed SSL servers. Here's the fix:
      you have to create an UnsafeClient class and then set it to retrofit's client.



      public class UnsafeOkHttpClient {  
      public static OkHttpClient getUnsafeOkHttpClient() {
      try {
      // Create a trust manager that does not validate certificate chains
      final TrustManager trustAllCerts = new TrustManager {
      new X509TrustManager() {
      @Override
      public void checkClientTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
      }

      @Override
      public void checkServerTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
      }

      @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]);
      builder.hostnameVerifier(new HostnameVerifier() {
      @Override
      public boolean verify(String hostname, SSLSession session) {
      return true;
      }
      });

      OkHttpClient okHttpClient = builder.build();
      return okHttpClient;
      } catch (Exception e) {
      throw new RuntimeException(e);
      }
      }
      }

      OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

      Retrofit.Builder builder = new Retrofit.Builder()
      .baseUrl("BASEURL")
      .client(okHttpClient);





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        I found the problem! It was a problem with self-signed SSL servers. Here's the fix:
        you have to create an UnsafeClient class and then set it to retrofit's client.



        public class UnsafeOkHttpClient {  
        public static OkHttpClient getUnsafeOkHttpClient() {
        try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager trustAllCerts = new TrustManager {
        new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
        }

        @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]);
        builder.hostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
        return true;
        }
        });

        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
        } catch (Exception e) {
        throw new RuntimeException(e);
        }
        }
        }

        OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

        Retrofit.Builder builder = new Retrofit.Builder()
        .baseUrl("BASEURL")
        .client(okHttpClient);





        share|improve this answer












        I found the problem! It was a problem with self-signed SSL servers. Here's the fix:
        you have to create an UnsafeClient class and then set it to retrofit's client.



        public class UnsafeOkHttpClient {  
        public static OkHttpClient getUnsafeOkHttpClient() {
        try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager trustAllCerts = new TrustManager {
        new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate chain, String authType) throws CertificateException {
        }

        @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]);
        builder.hostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
        return true;
        }
        });

        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
        } catch (Exception e) {
        throw new RuntimeException(e);
        }
        }
        }

        OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

        Retrofit.Builder builder = new Retrofit.Builder()
        .baseUrl("BASEURL")
        .client(okHttpClient);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 2:29









        Mohammad Hosein Kalantarian

        62




        62






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306992%2fhttp-failed-java-net-sockettimeoutexception-timeout-on-new-android-gradle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)