gpt4 book ai didi

java - 从列表中获取 URL 并传递给 HTTPGet 以发出 http 请求

转载 作者:行者123 更新时间:2023-12-02 05:01:36 24 4
gpt4 key购买 nike

请教各位。

我正在从数据库中读取信息,然后从中构造一个 Url。然后,我将 Url 保存到数组列表中。

我这样做的原因是为了进行异步 http 调用。

下面的代码是我创建列表的方法:

ArrayList TheList = new ArrayList();

    try {        

Connection conn = getConnection();
Statement st = conn.createStatement();
ResultSet srs = st.executeQuery("select distinct top 100 a.somename from sometable");

String urlbuild = "";
while (srs.next()) {
Columna cola = new Columna();

cola.setcola(srs.getString("somename "));


urlbuild = "http://xxxx:8080/xxxx/select?q=Names:" + '"' + java.net.URLEncoder.encode(srs.getString("somename "),"UTF-8") + '"' + "&wt=json&fl=id,colb,colc&start=0&rows=10000000";

cola.setcola(urlbuild);

TheList.add(cola);
}

下面是我尝试引用列表来进行 httpcall 的代码

public static void callhttp(List<Somename> TheList) throws Exception {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
try {
httpclient.start();
/*final HttpGet[] requests = new HttpGet[] {
new HttpGet("http://www.apache.org/"),
new HttpGet("https://www.verisign.com/"),
new HttpGet("http://www.google.com/")
};*/
//final CountDownLatch latch = new CountDownLatch(requests.length);
final CountDownLatch latch = new CountDownLatch(Thelist.size());

//for (final HttpGet request: requests) {
for (final HttpGet request : Thelist) {
httpclient.execute(request, new FutureCallback<HttpResponse>() {

但是,Eclipse 突出显示“TheList”,并出现类型不匹配错误:无法从元素类型 TheList 转换为 httpget。

这是有问题的行for(最终 HttpGet 请求:Thelist){

请帮忙

最佳答案

public static void callhttp(List<Somename> TheList) throws Exception {

方法中的第一行将 TheList 描述为 Somename 类型的列表。但在 for 循环中,您尝试使用 TheList 作为 HttpGet 类型的列表。

for (final HttpGet request : Thelist) {

将方法的第一行更改为:

public static void callhttp(List<HttpGet> TheList) throws Exception {

public static void callhttp(List<? extends HttpGet> TheList) throws Exception {

关于java - 从列表中获取 URL 并传递给 HTTPGet 以发出 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28224451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com