我正在從我的Android應用搜索Google,並返回結果。我發現的所有內容都指向Google網頁搜索API,該網頁現在顯示is deprecated,並正在被替換爲Custom Search API。用於Android的棄用Google網頁搜索API的替代
新的自定義搜索API只允許您搜索您爲其創建自定義搜索引擎的網站。我想像任何人通過谷歌搜索所有的互聯網。
我該怎麼做?
我正在從我的Android應用搜索Google,並返回結果。我發現的所有內容都指向Google網頁搜索API,該網頁現在顯示is deprecated,並正在被替換爲Custom Search API。用於Android的棄用Google網頁搜索API的替代
新的自定義搜索API只允許您搜索您爲其創建自定義搜索引擎的網站。我想像任何人通過谷歌搜索所有的互聯網。
我該怎麼做?
Android有一個很多的搜索功能 - 所有內置。
看吧:
http://developer.android.com/guide/topics/search/index.html
谷歌代碼和Android SDK的是兩個不同的東西。網絡搜索API是谷歌代碼和,和你提到的,確實已經有利於谷歌自定義搜索的棄用:
http://code.google.com/apis/customsearch/v1/overview.html
最後,這裏是一個很好的博客文章,告訴您如何調用必應/雅虎!從Android的網絡搜索:
http://www.codexperiments.com/java/2011/01/create-your-own-web-search-application/
坦率地說,Bing的API看起來很多比谷歌自定義搜索更好的挫折感。從Bing的API不限制您每天100條查詢的自定義搜索的事實開始:)
'希望有所幫助!
您可以Bing搜索API -
首先,你需要創建微軟帳戶,並得到一個賬號密碼,然後用它如下:
import android.os.AsyncTask;
import android.util.Log;
import org.apache.commons.codec.binary.Base64;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by Asaf on 08/06/2014.
*/
public class SearchAsyncTask extends AsyncTask<Void, Void, Void> {
private final String TAG = getClass().getName();
@Override
protected Void doInBackground(Void... params) {
try {
String bingUrl = "https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27pinhassi%27";
String accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
String accountKeyEnc = new String(accountKeyBytes);
URL url = null;
url = new URL(bingUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
InputStream response = urlConnection.getInputStream();
String res = readStream(response);
Log.d(TAG, res);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
return null;
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
//System.out.println(line);
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
我會能夠使用它們來回報搜索結果到我的應用程序,以便我可以以任何我想要的方式顯示它們? – Peter 2011-12-19 03:23:19
當然可以。 Bing API使用JSON,btw ... – paulsm4 2011-12-19 04:05:21