Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Https post在Android emulator上工作,但在设备上不工作_Android_Android Emulator - Fatal编程技术网

Https post在Android emulator上工作,但在设备上不工作

Https post在Android emulator上工作,但在设备上不工作,android,android-emulator,Android,Android Emulator,我正在尝试使用下面的代码将数据发布到Https URL。我已经登记了 它工作得很好。但它在设备中不起作用。可以 有人请帮帮我 public Boolean PostData(String data) throws IOException { Boolean response = false; TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { @Override pub

我正在尝试使用下面的代码将数据发布到Https URL。我已经登记了 它工作得很好。但它在设备中不起作用。可以 有人请帮帮我

  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上显示响应 发布数据

谢谢,
Mahesh

您使用过互联网许可吗

您必须将以下内容添加到清单文件:

<uses-permission android:name="android.permission.INTERNET" /> 


(仿真器可以很好地运行Internet,但是如果你想在真实设备上使用它,你必须使用许可)

并且你正在解析到计算机或外部服务器上的页面?会有什么问题@Arcdar你知道吗?实际上我不知道,我必须用调试器检查它,但我太忙了。我的想法是,当某些东西在Emulator上工作,但在真实设备上不工作时,它通常与您试图使用的某些资源链接,而这些资源并不存在。我的意思是,你知道你的代码工作得很好,因为它在模拟器上工作。现在,你必须问自己:我正在使用哪些可能不在设备上的资源?(我熟悉用http发布信息,但不熟悉安全性,所以我不能告诉你)所以。。为什么要构造一个
DefaultHttpClient
?事实上,在上一个版本中,我曾经使用httpclient发布到HTTP url。现在服务器是https的,我做了更改,只是错过了删除它。