package com.sabirjan.Tor;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import com.sabirjan.DrawUyghurWord.ConvertUL;

public class UyghurDic_TordinEzdax {

	
	public static final int HTTP_200 = 200;
	public String GetWordInfo(String keyWord,String type)
	{
		try
		{
			String query = URLEncoder.encode(keyWord, "utf-8");
			String url="http://dict.izda.com/?a=android&keyword="+query+"&type="+type;
			System.out.println("URL="+url);
			String tmp=requestByGet(url.trim());
			System.out.println("tmp="+tmp);
			ConvertUL cls=new ConvertUL();
			tmp=cls.GetULUyghurWord(tmp);
			if(tmp.trim().equals("none"))
			{
				return " ﻣﯘﻧﺎﺳﯩﯟﻩﺗﻠﯩﻚ ﺋﯘﭼﯘﺭ ﺗﯧﭙﯩﻠﻤﯩﺪﻯ";
			}
			return tmp;
		}
		catch(Exception ex)
		{
			
		}
		return " ﻣﯘﻧﺎﺳﯩﯟﻩﺗﻠﯩﻚ ﺋﯘﭼﯘﺭ ﺗﯧﭙﯩﻠﻤﯩﺪﻯ";
	}
	
	public String sendGetRequest(String urlStr) {  
		
			String result = null;  
			if (urlStr.startsWith("http://")) {  
			try {  
			// Construct data  
			StringBuffer data = new StringBuffer();  
			// Send data  
			URL url = new URL(urlStr);  
			
			URLConnection conn = url.openConnection();  
			// Get the response  
			BufferedReader rd = new BufferedReader(new InputStreamReader(  
			conn.getInputStream()));  
			StringBuffer sb = new StringBuffer();  
			String line;  
			while ((line = rd.readLine()) != null) {  
			sb.append(line);  
			}  
			rd.close();  
			result = sb.toString();  
			} catch (Exception e) {  
			}}  

			return result;  
			} 
	
	public  String  requestByGet(String path) throws Exception {  
	    // 新建一个URL对象  
	    URL url = new URL(path);  
	    String strtmp="";
	    // 打开一个HttpURLConnection连接  
	    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();  
	    // 设置连接超时时间  
	    urlConn.setConnectTimeout(5 * 1000);  
	    // 开始连接  
	    urlConn.connect();  
	    // 判断请求是否成功  
	    if (urlConn.getResponseCode() == HTTP_200) {  
	        // 获取返回的数据  
	        byte[] data = readStream(urlConn.getInputStream());  
	       strtmp=new String(data, "UTF-8");
	    } 
	    // 关闭连接  
	    urlConn.disconnect();
	    return   strtmp;
	}  // 获取连接返回的数据
	
	private  byte[] readStream(InputStream inputStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			baos.write(buffer, 0, len);
		}
		byte[] data = baos.toByteArray();
		inputStream.close();
		baos.close();
		return data;
	}

	
}
