search

Home  >  Q&A  >  body text

Android HttpUrlConnection not working

Basically, I'm trying to connect to a web interface through an Android application.

I successfully used HttpClient to send commands to the form. However, I want to do this using HttpUrlConnection as recommended here http://android-developers.blogspot.com/2011/09/androids-http-clients.html, with the aim of getting a faster and more energy efficient connection.

URL url = new URL("http://" + mIpAddress + ":" + mPort + "/command.html");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);

OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(URLEncoder.encode("parameter=" + value, "UTF-8");
writer.flush();
writer.close();
os.close();

connection.connect();

EDIT: No exception is thrown because the code executes fine, maybe the request is not in the format expected by the server?

P粉953231781P粉953231781403 days ago714

reply all(1)I'll reply

  • P粉364129744

    P粉3641297442023-10-24 14:35:12

    POST requires connection.getInputStream() to work. It has been fixed.

    reply
    0
  • Cancelreply