我搜索了很多并想出了这个
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = tv_question.getText().toString();
intent.putExtra(SearchManager.QUERY, term);
startActivity(intent);
但我不想从浏览器打开我的结果。有什么方法可以将谷歌搜索结果第1页的结果变成任何格式的字符串。
答案 0 :(得分:0)
你这样做的方式只允许你启动搜索而不是获得任何结果,你可以使用
CustomSearch Api
https://developers.google.com/apis-explorer/#search/customsearch/customsearch/v1/search.cse.list
来自谷歌的并用Json解析结果
这是一个如何使用的摘录:
public searchQuery {
String key="your API key";
String qry="Android";// your keyword to search
URL url = new URL(
"https://www.googleapis.com/customsearch/v1?
key="+key+"&cx="Replace with unique ID"&q="+ qry +
"&alt=json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println(url);
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
if(output.contains("\"link\": \"")){
String link=output.substring(output.indexOf("\"link\": \"")+
("\"link\": \"").length(), output.indexOf("\","));
System.out.println(link);
}
}
conn.disconnect();
}
快乐编码!!