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

Getting URLs from a list to pass to HTTPGet to make an http request

提问的人。

我正在从数据库中读取信息,然后从中构建一个 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 {