要向网站发出请求,您可以使用Android中的HttpClient类或HttpURLConnection类。以下是使用HttpURLConnection类进行请求的示例代码:
```
URL url = new URL("https://www.example.com"); // 网站的URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); // 设置请求方法,如GET或POST
connection.setConnectTimeout(5000); // 设置连接超时时间
connection.setReadTimeout(5000); // 设置读取超时时间
int responseCode = connection.getResponseCode(); // 获取响应码
if (responseCode == HttpURLConnection.HTTP_OK) {
// 请求成功
InputStream inputStream = connection.getInputStream();
// 使用inputStream读取服务器返回的数据
} else {
// 请求失败
}
connection.disconnect(); // 断开连接
```
注意,从Android 9开始,使用明文HTTP请求(而非HTTPS)需要在`AndroidManifest.xml`文件中添加网络安全配置:
```
```
另外,您还可以使用第三方库如OkHttp来简化网络请求的操作。