Android HttpUrlConnection not working
P粉953231781
P粉953231781 2023-10-23 20:44:21
0
1
713

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粉953231781
P粉953231781

reply all(1)
P粉364129744

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template