package com.sabirjan.liulanqi.Wangluo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.sabirjan.android.TSNews_Activity;

import android.app.Activity;
import android.content.Context;

public class HtmlChuli {

	HttpURLConnection con;
	
	
	class JianCha extends Thread
	{
		public void run()
		{
			while(!TSNews_Activity.isNew)
			{
				if(con!=null)
				{
					
				}
			}
			con.disconnect();
			
		}
	}
	
	public String GetHtmlCode(String strurl) throws Exception
	  {
		try {
	  	  	
		URL url=new URL(strurl);
		//new Thread(new JianCha()).start();
	    con=(HttpURLConnection) url.openConnection();
	  
	  	con.setRequestMethod("GET");
	  	con.setReadTimeout(100*1000);
	  	InputStream isStream=con.getInputStream();
	  
	
			
	  	byte []data=readInputStream(isStream);
	  	String html = new String(data, "UTF-8");
		return html;
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		}
	  }
	 
	public String getErrorPage(Context con) throws Exception
	{
		String strHtml="";
		try
		{
			
			InputStream isStream=con.getAssets().open("12.htm");
	        
	    	byte []data=readInputStream(isStream);
	    	 strHtml = new String(data, "UTF-8");
		}
		catch(Exception ex)
		{
			throw new Exception(ex.getMessage());
		}
		return strHtml;
	}
	
	 private  byte[] readInputStream(InputStream inStream) throws Exception{
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while( (len=inStream.read(buffer)) != -1 ){
				outStream.write(buffer, 0, len);
			}
			inStream.close();
			return outStream.toByteArray();
		}

	  
	
	public String getHtmlData(String data,Activity context){
	  	
	  	if(data.length()>0)
	  	{
	  		data=data.toLowerCase();
	  		String data_1="";
	  		String data_2="";
	  		if(data.contains("<head>"))
	  		{
	  			data_1=data.substring(0,data.indexOf("</head>"));
	  			data_2=data.substring(data.indexOf("</head>"));
	  			//'file://"+ context.getFilesDir().getAbsolutePath()+ "/ALKATIPTor.TTF'
	  			String head = "<style>@font-face {font-family: 'Alpida Unicode System';src: url('file:///android_asset/fonts/ALKATIPTor.TTF');}body{font-family: 'Alpida Unicode System';text-align:right;}</style>";
	  
	  		//	String head = "<style>@font-face {font-family: 'monospace';src: url('file:///android_asset/fonts/UKIJTuT.ttf') format('truetype');}body {font-family: 'monospace';}</style>";
	  		      data=data_1+head+"</head>"+data_2;
	  		      
	  		}
	  	}

	      return data;
	   }
}
