我希望在开关点击监听器中使用curl命令发送OFF指令,并进行登录验证,类似于http://admin:1234@10.1.1.83/outlet?4=OFF
我在开关上设置了点击监听器,当点击监听器时,我会调用一个curl方法。
switch4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getRequest();
}
});
现在,getRequest()方法的代码如下...
public static String getRequest() {
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufferedReader = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost();
URI uri = new URI("http://10.1.1.83/outlet?4=OFF");
httpPost.setURI(uri);
httpPost.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("admin", "1234"),
HTTP.UTF_8, false));
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
String readLine = bufferedReader.readLine();
while (readLine != null) {
stringBuffer.append(readLine);
stringBuffer.append("\n");
readLine = bufferedReader.readLine();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO: handle exception
}
}
}
return stringBuffer.toString();
}
libcurl
的端口适用于Android,但可能会有。 "阅读我的代码和描述" -- 您的代码和描述中没有包含问题。如果您的问题是“我该如何做到这一点?”,请解释您尝试过什么以及遇到了什么具体问题。 - CommonsWare