Android 安卓-我如何启动网络搜索并获得活动结果
我想构建一个应用程序,从用户那里接收短语,在web上搜索,并在应用程序中显示结果(这样会更舒适地显示) 到目前为止,我成功地启动了谷歌搜索,但无法将搜索结果返回到我的活动中。我尝试了Android 安卓-我如何启动网络搜索并获得活动结果,android,Android,我想构建一个应用程序,从用户那里接收短语,在web上搜索,并在应用程序中显示结果(这样会更舒适地显示) 到目前为止,我成功地启动了谷歌搜索,但无法将搜索结果返回到我的活动中。我尝试了StartActivityForResults,但谷歌搜索似乎不支持它。 我想以URL列表或类似的形式获取搜索结果,这样我就可以在我的应用程序的另一个活动中打开它。有什么建议吗 以下是我目前的代码: public void SearchWeb (View view) { Intent intent = new
StartActivityForResults
,但谷歌搜索似乎不支持它。
我想以URL列表或类似的形式获取搜索结果,这样我就可以在我的应用程序的另一个活动中打开它。有什么建议吗
以下是我目前的代码:
public void SearchWeb (View view) {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
EditText editText = (EditText) findViewById(R.id.edit_message); //get text from EditText
String query = editText.getText().toString();
query = query.concat(" chords");
intent.putExtra(SearchManager.QUERY, query);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
您这样做的方式只允许您启动搜索而不返回任何结果 您可以从google使用,并使用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();
}