【问题标题】:Android Application Google SearchAndroid 应用程序 Google 搜索
【发布时间】:2016-06-08 23:14:55
【问题描述】:

请有人帮我开发在后台结合谷歌搜索的android应用程序。

我是一个新手,我正在尝试设计一个 android 应用程序来自动跟踪我国的犯罪活动。有人告诉我在我的应用程序中使用谷歌自定义搜索。我已经获得了 api 密钥和搜索引擎 id,但我不知道如何实现它以获得所需的结果。

我一直在互联网上搜索,但无济于事。他们谈论的只是谷歌地图和其他我无法将其与我的问题联系起来的东西。

任何示例代码都将不胜感激。谢谢

【问题讨论】:

    标签: android google-custom-search


    【解决方案1】:

    我有一些代码用于搜索图像,也使用谷歌搜索引擎,它至少应该让你知道要寻找什么。

    这是我使用的主要进口产品

    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;
    

    这是使用搜索引擎的方法

    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;
        }
    }
    

    我记得当我这样做时文档令人困惑,但至少这对我有用。

    清单

    <?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>
    

    布局

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

    【讨论】:

    • 请给我看看清单文件和布局xml文件。提前感谢埃德温
    • manifest 或 layout 没有什么特别之处,但它们确实存在
    • @Edwin 超级迟到的问题,但你知道如果有数百万个搜索结果会发生什么吗?这不适合结果列表,对吗?我对做类似的事情很感兴趣,但我只想要谷歌搜索的结果数量,而不是实际结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多