在Android应用中获取谷歌的搜索结果

时间:2012-09-02 18:41:48

标签: android

在我的Android应用程序中,我想从用户那里获取搜索查询,并使用该查询搜索谷歌,获取搜索结果并使用搜索结果填充列表。自定义搜索API限制为每天100次免费搜索。那么搜索还有其他选择吗?

2 个答案:

答案 0 :(得分:8)

这是你可以使用的东西。

http://google.com/complete/search?output=toolbar&q=查询

它返回一个XML文件。解析该xml以获得结果。 但谷歌可能会在未来更改查询的格式。这是唯一关注的问题。否则它很有效。

为了将来参考,请注意以下有关其他有用网站的查询。有些以JSON格式返回,有些则以XML格式返回。

http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=查询&安培; ALT = JSON

http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=查询

http://en.wikipedia.org/w/api.php?action=opensearch&search=查询&安培;限制= 10安培;命名空间= 0&安培;格式= JSON

http://anywhere.ebay.com/services/suggest/?q=查询&安培; S = 0

http://completion.amazon.com/search/complete?method=completion&q=查询&安培;搜索别名= APS&安培; MKT = 1

http://api.bing.net/osjson.aspx?Query=查询和Market = EN-US

答案 1 :(得分:1)

您可以尝试使用此代码

MainActivity.java

private EditText editTextInput;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g__search);

editTextInput = (EditText) findViewById(R.id.editTextInput);
}

public void onSearchClick(View v)
{
try {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
String term = editTextInput.getText().toString();
intent.putExtra(SearchManager.SUGGEST_URI_PATH_QUERY, term);
startActivity(intent);
} catch (Exception e) {
// TODO: handle exception
}

}

Activity_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >


<EditText
    android:id="@+id/editTextInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/editTextInput"
    android:layout_below="@+id/editTextInput"
    android:layout_marginRight="43dp"
    android:layout_marginTop="60dp"
    android:onClick="onSearchClick"
    android:text="CLICK" />

还添加互联网权限