在不使用POJO类的情况下,可以使用原始的Java代码来发送和处理HTTP请求。下面是一个使用Java的URLConnection类发送GET请求的示例代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpGetExample {
public static void main(String[] args) {
try {
// 设置请求URL
String url = "http://example.com/api/endpoint";
// 创建URL对象
URL obj = new URL(url);
// 打开连接
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为GET
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: " + response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
上述代码使用URLConnection类来发送GET请求,并读取响应内容。你可以根据需要修改代码来发送其他类型的请求,例如POST请求。
请注意,这种方法需要手动处理HTTP请求和响应的所有细节,包括设置请求方法、请求头、请求参数、处理响应代码和读取响应内容等。这种方式比使用POJO类更加底层,但也更加灵活。