Adjei Bernice
Adjei Bernice

Reputation: 19

Android Application Google Search

Please can someone help me with development of android application which incorporates google search at the background.

I am a newbie and I am trying to design an android application to track crimes in my country automatically. Someone told me to use the google custom search in my application. I have obtained the api key and search engine id but I have no idea how to implement it to get the desired results.

I have been searching all over the internet but to no avail. All they talk about is google maps and others which i can't relate it to my problem.

Any sample code will be gladly appreciated. Thanks

Upvotes: 0

Views: 373

Answers (1)

Edwin
Edwin

Reputation: 836

I have some code which I used to search for images also using the google search engine, it should at least give you an idea on what to look for.

this are the main imports I uses

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.model.Result;
import com.google.api.services.customsearch.model.Search;

and this is the method that uses the search engine

public List<Result> getSearchResult(String keyword){
    // Set up the HTTP transport and JSON factory
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    //HttpRequestInitializer initializer = (HttpRequestInitializer)new CommonGoogleClientRequestInitializer(API_KEY);
    Customsearch customsearch = new Customsearch.Builder(
                new NetHttpTransport(),
                new JacksonFactory(),
                null)
            .setApplicationName("RandomImage")
            .build();

    List<Result> resultList = null;
    try {
        Customsearch.Cse.List list = customsearch.cse().list(keyword);
        list.setKey(API_KEY);
        list.setCx(SEARCH_ENGINE_ID);
        list.setSafe("off");
        list.setSearchType("image");
        list.setImgSize("medium");
        list.setNum(3L);

        Search results = list.execute();
        resultList = results.getItems();

    }catch (Exception e) {
        e.printStackTrace();
    }

    if (resultList == null) {
        return new ArrayList<Result>();
    } else {
        return resultList;
    }
}

I remember the documentation being confusing when I did this, but at least this is what worked for me.

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stochastic.randomimage" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.stochastic.randomimage.MainActivity"
            android:label="Random Image" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Layout

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Upvotes: 1

Related Questions