I am using Google Custom Search API in Java to get results of Google in response to a query. I have written this code with the help of other posts, code is as follows:
First off, Google says: "The query parameters you can use with the JSON/Atom Custom Search API are summarized in this section. All parameter values need to be URL encoded." https://developers.google.com/custom-search/v1/using_rest#query-params Meaning that everything after the "?" should be encoded with an equivalent of the php url encoder which sets the standard for urlencoding. Thing is that Java's class URLEncoder isn't quite right, you have to do a couple of replaceAll's. You need to do this to your input:
String queryArguments = "key="+key+ "&cx="+ cx +"&q="+ searchText+"&alt=json"+"&start="+"0"+"&num="+"30";
Notice how there are quotes around the numbers. If you get these from variables use the following:
String thenum = Integer.toString(theinteger);
And then the proper encoding
String addition = URLEncoder.encode(queryArguments, "UTF-8")
.replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\+", "%20")
.replaceAll("\\%27", "'")
.replaceAll("\\%21", "!")
.replaceAll("\\%7E", "~");
Then you add that to the original unencoded url:
String url = "https://www.googleapis.com/customsearch/v1?"
String total = url + addition;
In conclusion your code will look like this:
String query = URLEncoder.encode("key="+key+ "&cx="+ cx +"&q="+ searchText+"&alt=json"+"&start="+"0"+"&num="+"30"), "UTF-8").replaceAll("\\%28", "(")
.replaceAll("\\%29", ")")
.replaceAll("\\+", "%20")
.replaceAll("\\%27", "'")
.replaceAll("\\%21", "!")
.replaceAll("\\%7E", "~");
URL url = new URL("https://www.googleapis.com/customsearch/v1?" + query);
HttpURLConnection conn2 = (HttpURLConnection) url.openConnection();
System.out.println("Connection opened!");
conn2.setRequestMethod("GET");
conn2.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn2.getInputStream())));
I hope this works for you. I did something very similar with the old deprecated image api, but the concept holds the same and I looked at the new docs. :)
EDIT: Make sure that your num parameter is between 0 and 10 inclusive.