简体   繁体   English

Android 4.4.2 Kitkat无法使用HttpURLConnection发送请求

[英]Android 4.4.2 Kitkat can not send a request with HttpURLConnection

In my app I send a POST request to a server via an HttpURLConnection. 在我的应用程序中,我通过HttpURLConnection将POST请求发送到服务器。 But since I updated my Samsung S4 to Android 4.4.2, I don't receive any requests in the server. 但是,由于我将Samsung S4更新为Android 4.4.2,因此服务器上未收到任何请求。

Here is my code: 这是我的代码:

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 ();
  }
}

Am I doing something wrong? 难道我做错了什么? Is there any HttpURLConnection change for KitKat? KitKat是否有HttpURLConnection更改?

When I use DefaultHttpClient everything works fine and I can receive the request by the server. 当我使用DefaultHttpClient时,一切正常,我可以通过服务器接收请求。 But I still like to use the HttpURLConnection, because the app is testet with this kind of connection. 但是我仍然喜欢使用HttpURLConnection,因为该应用程序具有这种连接方式。

Be aware if you're using emulator. 请注意,如果您使用的是模拟器。 I had such problem as I was using Proxy. 我在使用代理时遇到了这样的问题。 BTW. 顺便说一句。 have you added to your manifest: 您是否已将清单添加到清单中:

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

Also to work with response use corresponding class http://developer.android.com/reference/org/apache/http/HttpResponse.html 也可以使用响应使用相应的类http://developer.android.com/reference/org/apache/http/HttpResponse.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android无法使用HttpURLConnection发送GET请求 - Android can't send GET request with HttpURLConnection Java,安卓应用程序。 无法发送发布请求 HttpURLConnection conn - Java , android app. Can not send post request HttpURLConnection conn Android KitKat HttpURLConnection断开AsyncTask - Android KitKat HttpURLConnection disconnect AsyncTask Android kitkat v4.4.2上的Webview中的WebSocket客户端无法发送消息 - WebSocket client in Webview on Android kitkat v4.4.2 is not able to send messages AlarmManager在Android版本4.4.2 KitKat中触发两次 - AlarmManager fires twice in Android Version 4.4.2 KitKat android开关样式不适用于KitKat(4.4.2) - android switch style is not applied on KitKat (4.4.2) 在webview android kitkat 4.4.2上“选择要上传的文件” - “choose file to upload” on webview android kitkat 4.4.2 在Android 4.4.2 KitKat中使用moveTaskToBack覆盖后退按钮 - Override back button with moveTaskToBack in Android 4.4.2 KitKat Android Kitkat 4.4.2:自适应播放对SurfaceFlinger的影响 - Android Kitkat 4.4.2: Adaptive Playback impact on SurfaceFlinger Android Kitkat 4.4.2 ImageView setBackgroundResource无法正常工作? - Android Kitkat 4.4.2 ImageView setBackgroundResource not working?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM