初始化
public static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象 getSchemeRegistry()
static {
client.setTimeout(60000); // 设置链接超时,如果不设置,默认为10s
}
加载KeyStore
public static SchemeRegistry getSchemeRegistry() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
// trustStore.load();
cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
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));
return registry;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (CertificateException e) {
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
return null;
} catch (KeyStoreException e) {
e.printStackTrace();
return null;
} catch (KeyManagementException e) {
e.printStackTrace();
return null;
}
}
public static void postNoHead(String url, RequestParams params, Context context,
AsyncHttpResponseHandler responseHandler) {
if (NetworkUtils.isNetworkAvailable(context)){
try {
client.setURLEncodingEnabled(false);
client.post(context,url, params, responseHandler);
Log.e("", "post--url--"+ url);
} catch (Exception e) {
e.printStackTrace();
}
} else {
ToastUtils.showToast(context, R.string.l_net_setting_txt);
}
}
public static void getNoHead(String url, Context context,
AsyncHttpResponseHandler responseHandler) {
if (NetworkUtils.isNetworkAvailable(context)) {
try {
client.setURLEncodingEnabled(false);
client.get(url, null, responseHandler);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("", "get--url--" + url);
} else {
ToastUtils.showToast(context, R.string.l_net_setting_txt);
}
}
阅读更多
资本寒冬下的android面经
Android自定义View——啥是佩奇?
Flutter终将逆袭!1.2版本发布,或将统一江湖
APK 的前世今生:从 Android 源码到 apk 的编译打包流程
网友评论