검색 엔진은 아니더라도, twitter에도 검색 하는  api를 제공하고 있어서 만들어본 twitter 검색하는 방법.

트위터는 아주 바람직한 서비스인것 같다, 어떤 트윗들이 요새 떠다니는지 검색도 할수 있게 해주고, 

또 여러가지 옵션으로 원하는대로 검색을 할수 있으니!!!


그리고 JSON을 지원해줌..(나는 JSON을 너무 사랑함ㅋㅋㅋㅋ, 영어 이름을 jason이라고 지을까 싶다..)


https://dev.twitter.com/docs/api/1/get/search  --> 너무나 간단한 Twitter 검색 API 사용법.



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 TwitterSearch implements ISearch
{
	private static final String TAG = "TwitterSearch";
	private static final String BASE_URL = "https://search.twitter.com/search.json";
	private static final String[] TARGETS = new String[]{"?"};
		
	private static final String RESULT_TYPE ="recent";
	private static final class PARAM{		
		private static final String QUERY = "q=";// always start with this parameter!!!		
		private static final String RESULT_SIZE = "&rpp=";
		private static final String RESULT_TYPE = "&result_type=";		
	}	
	private static final String RESULTS = "results";				// json array.
	private static final String TEXT = "text";				// 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();		
		
		final StringBuilder builder = new StringBuilder();
		for(String target : TARGETS)
		{
			URL url = null;
			try {
				url = new URL(	BASE_URL + target+									
									PARAM.QUERY + URLEncoder.encode(query, "UTF-8") + 
									PARAM.RESULT_TYPE+RESULT_TYPE+
									PARAM.RESULT_SIZE+resultSize*20);
			} 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 JSONArray resultArray = json.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(TEXT);					
					analyzer.analyze(content,mAnalyzedList);				
				}
				Log.e(TAG,"Google Result END ============================================================");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return mAnalyzedList;		
	}	
}


저작자 표시
신고

+ Recent posts

티스토리 툴바