java - Searching google programatically without ajax -
apparently google retired ajax api have been using in java android, dug through new api page can't find seems work in java, or non-web language matter.
this had previously:
@override protected string doinbackground(string... query) { try { bufferedreader reader = new bufferedreader(new inputstreamreader(new url("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + urlencoder.encode(query[0], "utf-8")).openstream())); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.tostring(); } catch(exception e){e.printstacktrace(); return "failed anything"; } }
simply put, string supplied, , appended ajax search. results, in json form, processed string builder , returned whole string.
if attempt now, returns result:
{"responsedata": null, "responsedetails": "the google web search api no longer available. please migrate google custom search api (https://developers.google.com/custom-search/)", "responsestatus": 403}
again, have read custom search api have not found work outside javascript.
i'd prefer json solution, since that's i'm using can work whatever get.
so, there alternative can use similar method with? or going forced search engine?
//edit
okay, have fixed code. requires api key, can't posting in open source project, , limited amount of searches unless pay, i'm going forced go competitor. here fixed code interested:
@override protected string doinbackground(string... query) { try { string apikey = "insert_your_api_key"; string customsearchengine = "insert_your_custom_search_key"; bufferedreader reader = new bufferedreader(new inputstreamreader(new url("https://www.googleapis.com/customsearch/v1?key=" + apikey + "&cx=" + customsearchengine + "&q=" + urlencoder.encode(query[0], "utf-8")).openstream())); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line).append("\n"); } reader.close(); return sb.tostring(); } catch(exception e){e.printstacktrace(); return "failed anything"; } }
they mention json api on site here
it rest api. google "java rest client" examples on how use java
Comments
Post a Comment