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

时间:2015-01-29 20:56:32

标签: java asynchronous http-get

问题小伙伴。

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

我这样做的原因是进行asynchronus http调用。

以下代码是我创建List的方式:

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){

请帮助

1 个答案:

答案 0 :(得分:2)

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 {