Skip to content

Commit c7ebff8

Browse files
author
XYG6
committed
REFACTORING - compress code
1 parent 0209f80 commit c7ebff8

File tree

3 files changed

+9
-32
lines changed

3 files changed

+9
-32
lines changed

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android {
55
buildToolsVersion '23.0.1'
66

77
defaultConfig {
8-
minSdkVersion 3
8+
minSdkVersion 9
99
targetSdkVersion 23
1010
consumerProguardFiles 'proguard.txt'
1111
}

library/src/main/java/com/loopj/android/http/MySSLSocketFactory.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
* certificate validation on every device, use with caution
6060
*/
6161
public class MySSLSocketFactory extends SSLSocketFactory {
62-
SSLContext sslContext;
62+
final SSLContext sslContext = SSLContext.getInstance("TLS");
6363

6464
/**
6565
* Creates a new SSL Socket Factory with the given KeyStore.
@@ -72,18 +72,6 @@ public class MySSLSocketFactory extends SSLSocketFactory {
7272
*/
7373
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
7474
super(truststore);
75-
76-
// Define sslContext
77-
try {
78-
sslContext = SSLContext.getInstance("TLSv1.2");
79-
Log.w("SSLSocketFactory", "TLSv1.2 is supported");
80-
} catch (NoSuchAlgorithmException e) {
81-
// TODO fallback v1.1 if needed
82-
Log.w("SSLSocketFactory", "TLSv1.2 is not supported in this device; falling through TLSv1.0");
83-
sslContext = SSLContext.getInstance("TLSv1");
84-
// should be available in any device; see reference of supported protocols in
85-
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
86-
}
8775

8876
X509TrustManager tm = new X509TrustManager() {
8977
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
@@ -212,28 +200,17 @@ public Socket createSocket() throws IOException {
212200
enableSecureProtocols(socket);
213201
return socket;
214202
}
215-
203+
216204
/**
217205
* Activate supported protocols on the socket.
218-
*
206+
*
219207
* @param socket The socket on which to activate secure protocols.
220208
*/
221209
private void enableSecureProtocols(Socket socket) {
222-
// get supported params
210+
// set all supported protocols
223211
SSLParameters params = sslContext.getSupportedSSLParameters();
224-
String [] supportedProtocols = params.getProtocols();
225-
226-
// activate supported protocols on the socket
227-
try {
228-
Socket localSocket = sslContext.getSocketFactory().createSocket();
229-
((SSLSocket) localSocket).setEnabledProtocols(supportedProtocols);
230-
}catch (Exception e)
231-
{
232-
233-
}
234-
235-
//((SSLSocket) socket).setEnabledProtocols(new String[] {"TLSv1.2"} );
236-
}
212+
((SSLSocket) socket).setEnabledProtocols(params.getProtocols());
213+
}
237214

238215
/**
239216
* Makes HttpsURLConnection trusts a set of certificates specified by the KeyStore
@@ -242,4 +219,4 @@ public void fixHttpsURLConnection() {
242219
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
243220
}
244221

245-
}
222+
}

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android {
1212
buildToolsVersion '23.0.1'
1313

1414
defaultConfig {
15-
minSdkVersion 3
15+
minSdkVersion 9
1616
targetSdkVersion 23
1717
}
1818

0 commit comments

Comments
 (0)