簡體   English   中英

替換已棄用的Android版Google網絡搜索API

[英]Replacement for deprecated Google web search API for Android

我正在尋找從我的Android應用中搜索Google並返回結果的方法。 我發現的所有內容都指向Google Web Search API,該頁面現在說的已棄用,並已由Custom Search API取代。

新的自定義搜索API僅允許您搜索已為其創建自定義搜索引擎的網站。 我想像搜索Google一樣搜索所有互聯網。

我怎樣才能做到這一點?

Android具有很多搜索功能-全部內置。

看這里:

http://developer.android.com/guide/topics/search/index.html

Google Code和Android SDK是兩件事。 Web搜索API是Google Code,並且您已經注意到,它確實已被Google自定義搜索棄用:

http://code.google.com/apis/customsearch/v1/overview.html

http://www.google.com/cse/

最后,這是一個不錯的博客條目,向您展示了如何調用Bing / Yahoo!。 通過Android進行網絡搜索:

http://www.codexperiments.com/java/2011/01/create-your-own-web-search-application/

坦白說,Bing API看起來要比Google自定義搜索好得多。 首先,Bing的API不會像“自定義搜索”一樣將您限制為每天100個查詢:)

希望有幫助!

您可以Bing搜索API-

首先,您需要在Microsoft創建一個帳戶並獲取一個帳戶密鑰,然后按以下方式使用它:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM