能否再提供一个不使用okhttp的例子?
时间: 2023-03-19 09:24:36 浏览: 67
当然可以!以下是使用Java内置的HttpURLConnection进行HTTP GET请求的示例代码:
```java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class HttpGetExample {
public static void main(String[] args) throws IOException {
String url = "https://jsonplaceholder.typicode.com/posts/1";
URL urlObj = new URL(url);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
```
该代码会向 https://jsonplaceholder.typicode.com/posts/1 发送一个HTTP GET请求,并打印出响应的状态码和内容。你可以将URL更改为你想要请求的网址。