// 使用 AsyncTask 进行网络请求
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
// 这里进行网络请求操作
try {
URL url = new URL("");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
// 处理服务器响应的数据
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "网络请求结果";
}
@Override
protected void onPostExecute(String result) {
// 在这里更新UI
}
}.execute();