如何在不打开浏览器的情况下发送简单的http命令?
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://192.168.1.95:8080/json.htm?type=command¶m=switchlight&idx=2&switchcmd=Off&level=0"));
startActivity(browserIntent);
}
});
答案 0 :(得分:0)
我想,我知道你想做什么。一个简单的例子here可能会有所帮助。
答案 1 :(得分:0)
HTTPClient
正是您要找的。确保在后台线程中使用它,例如在AsyncTask
。
这样的教程可以帮助您入门:http://hmkcode.com/android-internet-connection-using-http-get-httpclient/
答案 2 :(得分:0)
同样的问题Make an HTTP request with android
简单来说,这是代码(http://www.androidhive.info/2011/10/android-making-http-requests/)
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(
"http://www.example.com/login");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
nameValuePair.add(new BasicNameValuePair("message",
"Hi, trying Android HTTP post!"));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}