要在Apache HttpClient中反序列化响应,您可以使用Jackson库来处理JSON响应,或使用JSoup来处理HTML响应。以下是这两种方法的示例代码:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
import java.io.InputStream;
public class HttpClientExample {
    public static void main(String[] args) {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("http://example.com/api/endpoint");
        try {
            HttpResponse response = httpClient.execute(request);
            InputStream inputStream = response.getEntity().getContent();
            
            ObjectMapper objectMapper = new ObjectMapper();
            MyResponseObject responseObject = objectMapper.readValue(inputStream, MyResponseObject.class);
            
            // 使用反序列化后的响应对象进行进一步处理
            System.out.println(responseObject.getSomeProperty());
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class MyResponseObject {
    private String someProperty;
    public String getSomeProperty() {
        return someProperty;
    }
    public void setSomeProperty(String someProperty) {
        this.someProperty = someProperty;
    }
}
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class HttpClientExample {
    public static void main(String[] args) {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("http://example.com");
        try {
            HttpResponse response = httpClient.execute(request);
            Document document = Jsoup.parse(response.getEntity().getContent(), "UTF-8", "http://example.com");
            
            // 使用JSoup查询和处理HTML响应
            String title = document.title();
            System.out.println("Title: " + title);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
请确保项目中已添加所需的依赖项,例如Jackson库或JSoup库。