【问题标题】:Android 4.4.2 Kitkat can not send a request with HttpURLConnectionAndroid 4.4.2 Kitkat 无法使用 HttpURLConnection 发送请求
【发布时间】:2014-10-17 09:16:20
【问题描述】:

在我的应用程序中,我通过 HttpURLConnection 向服务器发送 POST 请求。但自从我将三星 S4 更新到 Android 4.4.2 后,我在服务器中没有收到任何请求。

这是我的代码:

private static void send (final String uri, String SomeText) throws IOException {
  final URL url = new URL (uri);
  final HttpURLConnection conn;

  // setup the connection
  conn = (HttpURLConnection) url.openConnection ();
  conn.setDoInput (true);
  conn.setDoOutput (true);
  conn.setUseCaches (false);
  conn.setRequestMethod ("POST");
  conn.setChunkedStreamingMode (0);

  final OutputStream os = conn.getOutputStream ();
  final OutputStreamWriter osw = new OutputStreamWriter (os);
  final BufferedWriter writer = new BufferedWriter (osw);

  try {

    writer.write (SomeText);

  } finally {
    writer.flush ();
    writer.close ();
    conn.disconnect ();
  }
}

我做错了吗? KitKat 的 HttpURLConnection 是否有任何更改?

当我使用 DefaultHttpClient 时,一切正常,我可以接收服务器的请求。但是我还是喜欢用HttpURLConnection,因为这个app是testet的这种连接方式。

【问题讨论】:

    标签: android http connection android-4.4-kitkat


    【解决方案1】:

    如果您使用的是模拟器,请注意。我在使用代理时遇到了这样的问题。顺便提一句。您是否已将其添加到清单中:

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

    也可以使用相应的类来处理响应 http://developer.android.com/reference/org/apache/http/HttpResponse.html

    【讨论】:

    • 权限已添加到此应用。该应用程序在 Android 4.2.2 上运行良好。问题出现在 Android 4.4.2 的版本更新中。顺便说一句,服务器是用另一种语言独立编程的,并在更新 android 手机之前收到所有请求。
    • 相当奇怪的尝试使用 HTTP 请求/响应而不是 HTTPURLConnection 可能会有所帮助......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 2013-12-04
    相关资源
    最近更新 更多