2012-09-02 37 views
2

在我的Android應用程序中,我想從用戶獲取搜索查詢,並使用該查詢搜索谷歌,獲取搜索結果並用搜索結果填充列表。自定義搜索API每天限制爲100次免費搜索。那麼搜索有沒有其他選擇?在android應用中獲取Google的搜索結果

回答

7

這是你可以使用的東西。

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=查詢 &市場= EN-US

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" /> 

另外補充許可互聯網