【问题标题】:Replacement for deprecated Google web search API for Android替换已弃用的适用于 Android 的 Google 网络搜索 API
【发布时间】:2012-01-23 07:31:49
【问题描述】:

我希望从我的 Android 应用中搜索 Google 并返回结果。我发现的所有内容都指向 Google Web Search API,该页面现在显示为is deprecated,并被Custom Search API 取代。

新的自定义搜索 API 仅允许您搜索已为其创建自定义搜索引擎的网站。我想像任何人一样通过谷歌搜索所有的互联网。

我该怎么做?

【问题讨论】:

    标签: android


    【解决方案1】:

    Android 有很多 搜索功能 - 都是内置的。

    看这里:

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

    Google Code 和 Android SDK 是两个不同的东西。 Web 搜索 API 是 Google 代码,而且您注意到,它确实已被弃用,取而代之的是 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 个查询:)

    '希望有帮助!

    【讨论】:

    • 我可以使用这些将搜索结果返回到我的应用程序,以便我可以以任何我想要的方式显示它们吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      相关资源
      最近更新 更多