简体   繁体   中英

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. But since I updated my Samsung S4 to Android 4.4.2, I don't receive any requests in the server.

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?

When I use DefaultHttpClient everything works fine and I can receive the request by the server. But I still like to use the HttpURLConnection, because the app is testet with this kind of connection.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM