Java URL Query search(String query, int numberOfUrls)

Here you can find the source of search(String query, int numberOfUrls)

Description

search

License

Apache License

Declaration

public static List<String> search(String query, int numberOfUrls) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Main {
    public static List<String> search(String query, int numberOfUrls) throws IOException {

        List<String> listOfURLs = new ArrayList<String>();

        // Setting up the google API URL
        String APIkey = "AIzaSyDNZ6K2wI2z2-j1tTSaZrpgUQJpOV2DRv8";
        query = URLEncoder.encode(query, "UTF-8");

        // Using google developer API key to use custom search
        String stringURL = "https://www.googleapis.com/customsearch/v1?key=" + APIkey
                + "&cx=013036536707430787589:_pqjad5hr1a&q=" + query + "&alt=json";
        URL url = new URL(stringURL);

        // System.out.println(stringURL);
        // Opening a connection and sending a request
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
        BufferedReader buffer;//from   ww  w . jav  a2 s .  com

        // To check for potential errors
        if (connection.getResponseCode() >= 400) {
            buffer = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
        } else {
            buffer = new BufferedReader(new InputStreamReader((connection.getInputStream())));
        }

        String bufferLine;
        int countOfURLs = 0;
        while ((bufferLine = buffer.readLine()) != null && countOfURLs < numberOfUrls) {

            if (bufferLine.contains("\"link\": \"")) {
                String link = bufferLine.substring(bufferLine.indexOf("\"link\": \"") + ("\"link\": \"").length(),
                        bufferLine.indexOf("\","));
                listOfURLs.add(link); // Will add the google search return links
                // to the our URL list.
                countOfURLs++;
            }
        }
        connection.disconnect();
        return listOfURLs;

    }
}

Related

  1. getHTTPQuery(String urlToRead)
  2. getQueryParams(String url)
  3. query(URL host, String endpoint, String customer, String name, OutputStream out)
  4. queryEndpoint(String url)
  5. queryHtml(String url)