我想发出一个简单的发帖请求,而我正在使用以下代码:
public void Encender_LED(View view){
try {
URL url = new URL("http://192.168.1.25");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String data = "/relay=on";
urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(data.getBytes().length);
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(data.getBytes());
out.flush();
out.close();
} catch (IOException e) {
Toast.makeText(this, "No ha sido posible realizar la petición", Toast.LENGTH_SHORT).show();
} finally {
if(urlConnection!=null){
urlConnection.disconnect();
}
}
}
尽管如此,我仍然遇到问题,并且出现一个警告对话框,其中显示文本“应用程序已停止”。它堆积在行中:
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
我已在AndroidManifest.xml中添加了Internet权限