2011-12-19 13 views
0

私のAndroidアプリからGoogleを検索して結果を返したいと思っています。私が見つけたものはすべて、Google Web Search APIを指しており、そのページは現在is deprecatedと書かれており、Custom Search APIに置き換えられています。Android用の非推奨のGoogleウェブ検索APIの代わりに

新しいカスタム検索APIを使用すると、カスタム検索エンジンを作成したウェブサイトでのみ検索することができます。私は誰もがGoogleを介してインターネットのすべてを検索したいです。

どうすればいいですか?

答えて

3

Androidはロットの検索機能を内蔵しています。

ここを見て:

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

はGoogle CodeとAndroid SDKは、二つの異なるものです。 Web検索API、それは確かにGoogleカスタム検索のために廃止された、Google Codeのです、そしてあなたが注意:

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

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

最後に、ここにあなたの方法を示して良いブログのエントリですビン/ヤフーを呼び出す!アンドロイドからウェブ検索:

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

は率直に言って、BingのAPIは、Googleカスタム検索よりも良いたくさんの一体を探します。 BingのAPIがカスタム検索のように1日あたり100クエリに制限されないという事実から始まって:

'それは助けてくれるでしょう!

+0

iはリターンにそれらのを使用することができるだろう検索結果は私のアプリケーションに表示されますので、どのような方法で表示してもかまいません。 – Peter

+0

はい、もちろんです。 Bing APIはJSON、btwを使用しています... – paulsm4

0

することはでき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(); 
    } 

} 
関連する問題