我正在尝试使用以下代码将数据发布到Https网址。我已经办理了入住手续 模拟器工作正常。但它不适用于Device。能够 有人请帮帮我。
public Boolean PostData(String data) throws IOException {
Boolean response = false;
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
}};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
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));
// Set verifier
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append("data").append("=").append(data);
byte[] postData = postDataBuilder.toString().getBytes("UTF-8");
URL postURL = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) postURL.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
// ------------------
OutputStream out = conn.getOutputStream();
// --------------------------
out.write(postData);
out.close();
return response;
}
private static class CustomizedHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
有没有办法在我们之后显示该URL上的响应 发布数据。
谢谢, 马赫什
答案 0 :(得分:0)
您是否使用过INTERNET权限?
您必须添加到清单文件中:
<uses-permission android:name="android.permission.INTERNET" />
(互联网可以从模拟器中正常工作,但如果您想在真实设备上使用它,则必须使用该权限)