package util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class HttpUtil {
   public static void sendHttpRequest(final String address,final HttpCallbackListener listener){
	   new Thread (new Runnable(){

		@Override
		public void run() {
			// TODO ×Ô¶¯Éú³ÉµÄ·½·¨´æ¸ù
			HttpURLConnection connection = null;
			try {
				URL url = new URL(address);
				connection = (HttpURLConnection)url.openConnection();//½«·þÎñÆ÷·¢ËÍÇëÇó
				connection.setRequestMethod("GET");//GET±íÊ¾´Ó·þÎñÆ÷»ñÈ¡Êý¾Ý£¬post±íÊ¾ÉÏ´«Êý¾Ý
				connection.setReadTimeout(80000);//¶ÁÈ¡³¬Ê±
				connection.setConnectTimeout(8000);//Á¬½Ó³¬Ê±
				InputStream in = connection.getInputStream();//µÃµ½·þÎñÆ÷·µ»ØµÄÁ÷
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				StringBuilder reponse = new StringBuilder();
			    String line;
				while((line=br.readLine()) != null){
					
					reponse.append(line);
				}
				if(listener != null){
				
					listener.onFinish(reponse.toString());
				}
			} catch (Exception e) {
				// TODO: handle exception
				if(listener != null){
					listener.onError(e);
				}
			}
			finally{
				if(connection != null){
					connection.disconnect();
				}
			
			}
		}
		   
	   }).start();
	   
	   
   }
}
