編輯:關於Android編程
直接給代碼吧,研究了幾天才搞定......
public static final String CLIENT_KET_PASSWORD = "Ku6OpqKDfN4=305790"; // public static String getNewHttpClient(String url) { try { // KeyStore trustStore = KeyStore.getInstance("PKCS12", "BC"); // trustStore // .load(PcPostApplication.getInstance().getAssets() // .open("abc.pfx"), CLIENT_KET_PASSWORD.toCharArray()); SSLSocketFactory sf = new SSLSocketFactoryEx(AppConfig.mKeyStore, AppConfig.CERTFILE_PASSWORD.toCharArray()); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); HttpClient client = null; String msg = ""; try { ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); client = new DefaultHttpClient(ccm, params); HttpGet hg = new HttpGet(url); HttpResponse response = client.execute(hg); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instreams = entity.getContent(); msg = convertStreamToString(instreams); } return msg; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } return ""; } public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = ""; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }上面的加載證書並請求,但是在這需要注意一個問題SSLSocketFactory需要自定義,看下面自定義的代碼:
public class SSLSocketFactoryEx extends SSLSocketFactory { SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryEx(KeyStore truststore, char[] arry) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(truststore); KeyManagerFactory localKeyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory .getDefaultAlgorithm()); localKeyManagerFactory.init(truststore, arry); KeyManager[] arrayOfKeyManager = localKeyManagerFactory.getKeyManagers(); TrustManager tm = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }; sslContext.init(arrayOfKeyManager, new TrustManager[] { tm }, new java.security.SecureRandom()); } @Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); } @Override public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } }
一、 JNI基礎介紹JNI is the Java Native Interface. It defines a way for managed code (writte
1. 什麼是Volley我們平時在開發Android應用的時候不可避免地都需要用到網絡技術,而多數情況下應用程序都會使用HTTP協議來發送和接收網絡數據。Android系
UITextField是輸入字符的視圖控件1是UIView的子類,具有與view一樣的屬性2字符輸入只能是單行輸入,不能換行,也不能多行輸入3與自己特有的屬性4 text
本系列文章提供簡單Android應用開發實例方法,文章步驟如下所示:1 獲取應用所需的數據源數據源一般來源於互聯網、個人搜集或者其他方式2 應用UI設計每個應用軟件都需要