在这里,您是Volley的工作解决方案:

在以单例代码创建队列之前:

public class VolleyServiceSingleton {

private RequestQueue mRequestQueue;

private HurlStack mStack;

private VolleyServiceSingleton(){

SSLSocketFactoryExtended factory = null;

try {

factory = new SSLSocketFactoryExtended();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (KeyManagementException e) {

e.printStackTrace();

}

final SSLSocketFactoryExtended finalFactory = factory;

mStack = new HurlStack() {

@Override

protected HttpURLConnection createConnection(URL url) throws IOException {

HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);

try {

httpsURLConnection.setSSLSocketFactory(finalFactory);

httpsURLConnection.setRequestProperty("charset", "utf-8");

} catch (Exception e) {

e.printStackTrace();

}

return httpsURLConnection;

}

};

mRequestQueue = Volley.newRequestQueue(YourApplication.getContext(), mStack, -1);

}

}

这是SSLSocketFactoryExtended:

public class SSLSocketFactoryExtended extends SSLSocketFactory

{

private SSLContext mSSLContext;

private String[] mCiphers;

private String[] mProtocols;

public SSLSocketFactoryExtended() throws NoSuchAlgorithmException, KeyManagementException

{

initSSLSocketFactoryEx(null,null,null);

}

public String[] getDefaultCipherSuites()

{

return mCiphers;

}

public String[] getSupportedCipherSuites()

{

return mCiphers;

}

public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException

{

SSLSocketFactory factory = mSSLContext.getSocketFactory();

SSLSocket ss = (SSLSocket)factory.createSocket(s, host, port, autoClose);

ss.setEnabledProtocols(mProtocols);

ss.setEnabledCipherSuites(mCiphers);

return ss;

}

public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException

{

SSLSocketFactory factory = mSSLContext.getSocketFactory();

SSLSocket ss = (SSLSocket)factory.createSocket(address, port, localAddress, localPort);

ss.setEnabledProtocols(mProtocols);

ss.setEnabledCipherSuites(mCiphers);

return ss;

}

public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException

{

SSLSocketFactory factory = mSSLContext.getSocketFactory();

SSLSocket ss = (SSLSocket)factory.createSocket(host, port, localHost, localPort);

ss.setEnabledProtocols(mProtocols);

ss.setEnabledCipherSuites(mCiphers);

return ss;

}

public Socket createSocket(InetAddress host, int port) throws IOException

{

SSLSocketFactory factory = mSSLContext.getSocketFactory();

SSLSocket ss = (SSLSocket)factory.createSocket(host, port);

ss.setEnabledProtocols(mProtocols);

ss.setEnabledCipherSuites(mCiphers);

return ss;

}

public Socket createSocket(String host, int port) throws IOException

{

SSLSocketFactory factory = mSSLContext.getSocketFactory();

SSLSocket ss = (SSLSocket)factory.createSocket(host, port);

ss.setEnabledProtocols(mProtocols);

ss.setEnabledCipherSuites(mCiphers);

return ss;

}

private void initSSLSocketFactoryEx(KeyManager[] km, TrustManager[] tm, SecureRandom random)

throws NoSuchAlgorithmException, KeyManagementException

{

mSSLContext = SSLContext.getInstance("TLS");

mSSLContext.init(km, tm, random);

mProtocols = GetProtocolList();

mCiphers = GetCipherList();

}

protected String[] GetProtocolList()

{

String[] protocols = { "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"};

String[] availableProtocols = null;

SSLSocket socket = null;

try

{

SSLSocketFactory factory = mSSLContext.getSocketFactory();

socket = (SSLSocket)factory.createSocket();

availableProtocols = socket.getSupportedProtocols();

}

catch(Exception e)

{

return new String[]{ "TLSv1" };

}

finally

{

if(socket != null)

try {

socket.close();

} catch (IOException e) {

}

}

List resultList = new ArrayList();

for(int i = 0; i < protocols.length; i++)

{

int idx = Arrays.binarySearch(availableProtocols, protocols[i]);

if(idx >= 0)

resultList.add(protocols[i]);

}

return resultList.toArray(new String[0]);

}

protected String[] GetCipherList()

{

List resultList = new ArrayList();

SSLSocketFactory factory = mSSLContext.getSocketFactory();

for(String s : factory.getSupportedCipherSuites()){

Log.e("CipherSuite type = ",s);

resultList.add(s);

}

return resultList.toArray(new String[resultList.size()]);

}

}

在此代码中,我简单地添加设备支持的所有密码,对我来说这是可行的),可能会帮助某人)(干杯)

ps无需在清单中添加安全网络配置参数。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐