Sai Sunder
Sai Sunder

Reputation: 1021

Get Search results from Google in android app

In my android application I want to get search query from user, and search google with that query, get search results and populate a list with the search results. Custom Search API limits to 100 free searches per day. So is there any alternative for searching?

Upvotes: 3

Views: 5086

Answers (2)

Anu
Anu

Reputation: 551

You can try using this code

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

Also add permission for internet

Upvotes: 1

rahul
rahul

Reputation: 6487

This is something which you can use.

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

It returns an XML file. Parse that xml to get the results. But google may change the format of the query in future. Thats the only concern here. Otherwise it works great.

For future reference, note the following queries for other useful websites. Some return in JSON and others in XML formats.

http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=query&alt=json

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

http://en.wikipedia.org/w/api.php?action=opensearch&search=query&limit=10&namespace=0&format=json

http://anywhere.ebay.com/services/suggest/?q=query&s=0

http://completion.amazon.com/search/complete?method=completion&q=query&search-alias=aps&mkt=1

http://api.bing.net/osjson.aspx?Query=query&Market=en-us

Upvotes: 8

Related Questions