我有一些代碼,我用搜索圖片也使用谷歌搜索引擎,它應該至少給你一個想法什麼尋找。
這是我使用
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文件。在此先感謝埃德溫 –
沒有什麼特別的清單或佈局,但他們是 – Edwin