Android Question javax.net.ssl.SSLException: Connection closed by peer[SOLVED]

ronell

Well-Known Member
Licensed User
im running this code in my app
B4X:
Dim job1 As HttpJob
job1.Initialize("test",Me)
 job1.Download("https://www.google.com")
ProgressDialogShow2("Loading...",False)
then job response throws this ..
B4X:
 javax.net.ssl.SSLException: Connection closed by peer
this error only occurs on android 4.4 or maybe even in lower android versions. is there a solution to this issue?

Thanks
 

Semen Matusovskiy

Well-Known Member
Licensed User
The easiest way to activate SSL under Android 4 is to use Google's provider.
If your app already uses Google Play Services, try following:

1) Update Google Play Services on your phone (this is very important)
2) In Starter service
a) call ActivateSSL inside Service_Create
b) add following subroutines (de-facto, written by Erel):

B4X:
Sub ActivateSSL

    Dim javaobjectContext                                                       As JavaObject
    Dim javaobjectInstance                                                      As JavaObject
    Dim objectListener                                                          As Object
    Dim phoneInstance                                                           As Phone

    Try
        Select Case phoneInstance.SdkVersion
            Case 16, 17, 18, 19, 20 ' Android 4.1 - 4.4
            Case Else
                Return
        End Select
        javaobjectInstance.InitializeStatic ("com.google.android.gms.security.ProviderInstaller")
        javaobjectContext.InitializeContext
        DisableStrictMode
        objectListener = javaobjectInstance.CreateEventFromUI ("com.google.android.gms.security.ProviderInstaller.ProviderInstallListener", "objectListener", Null)
        javaobjectInstance.RunMethod ("installIfNeededAsync", Array (javaobjectContext, objectListener))
        Wait For objectListener_Event (stringMethodName As String, objectArguments () As Object)
        ' If stringMethodName = "onProviderInstalled" Then [ Provider installed successfully ] Else [ Error installing provider (objectArguments (0)) ]
    Catch
    End Try

End Sub

Sub DisableStrictMode

    Dim javaobjectInstance                                                      As JavaObject
    Dim javaobjectPolicy                                                        As JavaObject
    Dim javaobjectStrictMode                                                    As JavaObject

    Try
        javaobjectInstance.InitializeStatic ("android.os.Build.VERSION")
        If javaobjectInstance.GetField ("SDK_INT") > 9 Then
            javaobjectPolicy = javaobjectPolicy.InitializeNewInstance ("android.os.StrictMode.ThreadPolicy.Builder", Null)
            javaobjectPolicy = javaobjectPolicy.RunMethodJO ("permitAll", Null). RunMethodJO ("build", Null)
            javaobjectStrictMode.InitializeStatic ("android.os.StrictMode"). RunMethod ("setThreadPolicy", Array (javaobjectPolicy))
        End If
    Catch
    End Try

End Sub
 
Upvote 0
Top