2

I have written a simple HTTPS client in Java to POST some data to a server. The server has a valid certificate signed by a trusted CA, so I didn't have to mess with any keystores or self-signed certificates. Here is the code:

try
{
    String data = "This is a test";

    SSLSocketFactory sslFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();

    URL url = new URL("https://my.server.name");

    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
    conn.setSSLSocketFactory(sslFactory);
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 

    wr.write(data);
    wr.flush();
    wr.close();

    InputStream ins = conn.getInputStream();
    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader in = new BufferedReader(isr);

    String inputLine;
    String result;

    while( (inputLine = in.readLine()) != null )
        result += inputLine;

    in.close();
}
catch( Exception e )
{
    e.printStackTrace();
}

This code works fine when run as part of a command line Java program, but it does not work correctly on Android (at least not in the emulator). I have traced the problem to this line:

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

The call to conn.getOutputStream() seems to hang until the connection times out on the server. It does not throw an exception. It just waits indefinitely until the connection times out on the server, and then moves on to wr.write(data) but this fails, of course, because the connection is now closed.

Does anyone know what the problem might be? I searched for issues regarding getOutputStream() on Android, but I did not find anything useful yet.

Thanks!

5
  • I should have mentioned that the app does have the "INTERNET" uses-permission in the manifest, so that's not the issue. Commented Jun 27, 2011 at 2:53
  • Seems to work for google.com ... Are you sure it's not a server issue? What exception gets thrown on android? Commented Jun 27, 2011 at 3:02
  • @adotout: Are you testing this under the Android emulator? I'm seeing the same behavior with google.com, right at the call to getOutputStream(). It doesn't throw an exception, it just hangs until the server times out the connection. Commented Jun 27, 2011 at 3:15
  • 1
    This appears to be a problem with the Android emulator. I installed the app on my Motorola Droid and everything works fine. This isn't really an answer to the original problem, but the problem is irrelevant now (at least to me) since I can test the app on my phone. Commented Jun 27, 2011 at 3:58
  • @Dave: can you please answer the question yourself and then accept that answer? Also, you need to accept answers to previous questions if they fix your problem. Commented May 17, 2012 at 10:36

1 Answer 1

2

This appears to be a problem with the Android emulator. I installed the app on my Motorola Droid and everything works fine. This isn't really an answer to the original problem, but the problem is irrelevant now (at least to me) since I can test the app on my phone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.