구글은 Cusom Search API라는 유료 검색 모델을 지금 엄청 밀고 있는 중인데, 검색  api를 사용하기 위해서 돈을 지불하면서 까지 사용해야 하는 의문이 생겨서, 이전까지 무료로 제공되던 web search api를 사용했다.(이것도 곧 ... 없어질지도 모르겠다. ㅠ.ㅠ)


구글이 자꾸 왜 그러는건지 .... Google App Engine도 과금정책을 바꿔서 기존까지 무료로도 이용이 가능하던 서비스들이 이제는 돈을 낼수 밖에 없는 모델로 계속 바꾸고 있는 것 같다.  



https://developers.google.com/web-search/docs/ --> 현재는 deprecated 되어서 구글의 정책에 의해서 지원이 중단될지도 모르지만, web search api 있는 곳


구글의 검색 api는 JSON을 역시 지원하고, 아래 굵은 표시 된 부분을 조심해서 사용해주면 된다. 버전 설정이라던지 사이즈 설정이라던지 하는 부분이 잘못되면 에러만 리턴된다.



package com.hopeisagoodthing.searchtool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;


public class GoogleSearch implements ISearch
{
	private static final String TAG = "GoogleSearch";
	private static final String BASE_URL = "https://ajax.googleapis.com/ajax/services/search/";
	private static final String[] TARGETS = new String[]{"web?","news?","blogs?"};
	private static final String VERSION="1.0";	
	private static final int START = 1;
	private static final class PARAM{
		private static final String VERSION = "v=";	// always start with this parameter!!!
		private static final String QUERY = "&q=";		
		private static final String START = "&start=";
		private static final String RESULT_SIZE = "&rsz=";
	}	
	private static final String RESPONSE_DATA = "responseData";	// json obj
	private static final String RESULTS = "results";				// json array.
	private static final String CONTENT = "content";				// json obj
	
	private final List<String> mAnalyzedList = new ArrayList<String>();
	
	@Override
	public List<String> getAnalyze(final String query, final int resultSize, final IAnalyzer analyzer) {		
		mAnalyzedList.clear();	


		int realResultSize = 8;
		if(resultSize < 9)
		{
			realResultSize = resultSize;
		}
		
		final StringBuilder builder = new StringBuilder();
		for(String target : TARGETS)
		{
			URL url = null;
			try {
				url = new URL(	BASE_URL + target+
									PARAM.VERSION+VERSION+
									PARAM.QUERY + URLEncoder.encode(query, "UTF-8") + 
									PARAM.START +START+
									PARAM.RESULT_SIZE+realResultSize);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
			
			builder.setLength(0);
			try {
				URLConnection connection = null;
				String line = null;			
				BufferedReader reader = null;
				connection = url.openConnection();
				//connection.addRequestProperty("Referer", /* Enter the URL of your site here */);
				reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				while((line = reader.readLine()) != null) {
					 builder.append(line);
				}				 
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				Log.e(TAG,"Google Result START============================================================");
				final JSONObject json = new JSONObject(builder.toString());
				final JSONObject resultsJson = json.getJSONObject(RESPONSE_DATA);
				final JSONArray resultArray = resultsJson.getJSONArray(RESULTS);
				
				final int arraySize = resultArray.length();
				for(int i = 0; i< arraySize ; i++)
				{
					final JSONObject result = resultArray.getJSONObject(i);
					final String content = result.getString(CONTENT);
					analyzer.analyze(content,mAnalyzedList);				
				}
				Log.e(TAG,"Google Result END ============================================================");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return mAnalyzedList;		
	}	
}
블로그 이미지

커뉴

이 세상에서 꿈 이상으로 확실한 것을, 인간은 가지고 있는 것일까?

,