最近遇到一个头疼的问题,android 网络编程,使用android 模拟器 https协议域名方式访问服务器 ,总是报 UnknownHostNameException ,如果用ip方式替换域名就没有返回值,也没报错。求各位大神指教啊。贴上我自己的方法和访问路径 大家帮帮忙。
public static String GetData(String URL) throws ClientProtocolException,
IOException { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects()
.penaltyLog().penaltyDeath().build()); String returns = ""; HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory
.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80)); DefaultHttpClient client = new DefaultHttpClient();
SingleClientConnManager mgr = new SingleClientConnManager(
client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr,
client.getParams());
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
HttpPost httpPost = new HttpPost(URL); HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
// 解析返回的内容
returns = EntityUtils.toString(response.getEntity());
} return returns;
} https://ibsbjstar.ccb.com.cn/app/B2CMainPlatV5?CCB_IBSVersion=V5&SERVLET_NAME=B2CMainPlatV5&TXCODE=100119只要手机模拟器返回跟浏览器返回数据相同就ok了