Cannot resolve symbol 'HttpClient'
时间: 2023-12-21 12:29:02 浏览: 33
Cannot resolve symbol 'HttpClient'是因为HttpClient已经在Android SDK 23中被弃用,需要使用HttpURLConnection或者第三方库OkHttp来替代。以下是两种解决方法:
1.使用HttpURLConnection发送HTTP请求
```java
try {
URL url = new URL("http://www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
// 处理输入流
}
} catch (IOException e) {
e.printStackTrace();
}
```
2.使用OkHttp发送HTTP请求
```java
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.example.com")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseStr = response.body().string();
// 处理响应字符串
}
} catch (IOException e) {
e.printStackTrace();
}
```