2016-06-08 106 views
-1

請有人可以幫助我開發Android應用程序,其中包含谷歌搜索在後臺。Android應用谷歌搜索

我是一個新手,我試圖設計一個android應用程序來自動跟蹤我國的犯罪行爲。有人告訴我在我的應用程序中使用google自定義搜索。我已經獲得了API密鑰和搜索引擎ID,但我不知道如何實現它以獲得期望的結果。

我一直在搜索整個互聯網,但無濟於事。他們所談論的只是谷歌地圖和其他我無法將其與我的問題聯繫起來的人。

任何示例代碼將很樂意欣賞。謝謝

回答

0

我有一些代碼,我用搜索圖片也使用谷歌搜索引擎,它應該至少給你一個想法什麼尋找。

這是我使用

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

請你能告訴我的清單文件和佈局xml文件。在此先感謝埃德溫 –

+0

沒有什麼特別的清單或佈局,但他們是 – Edwin