小黄鸡的Java调用

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
 
public class post_request {
	private String key;
	private String GET_URL = "http://api.simsimi.com/request.p?key=" + key + "&lc=en&ft=1.0&text=hi";
	List<String> keysList;
	
	//------------------------------------------------------
	//设置目标地点
	public boolean setUrl(String urlString) {
		boolean flag = false;
		try {
			GET_URL = urlString;
			flag = true;
		} catch (Exception e) {
			//Do nothing
		}
		return flag;
	}
	
	//-----------------------------------------------------
	//主要的方法,拉数据
    public void readContentFromGet() throws IOException{
        String getURL = GET_URL;
        URLConnection conn = new URL(getURL).openConnection();
        InputStream is = conn.getInputStream();
        String lines = new String();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));//设置编码,否则中文乱码
        while ((lines = reader.readLine()) != null){
            	//lines = new String(lines.getBytes(), "utf-8");
                System.out.println(lines);//getResultString(lines));
        }
    }
    
    //-----------------------------------------------------
    //从拉取的数据中分离出来需要的数据
    private String getResultString(String lineString) {
    	String resultString = new String();
    	/*
    	 * 100-OK.
			400-Bad Request.
			401-Unauthorized.
			404-Not found.
			500-Server Error.
    	 */
    	try{
    		String[] temp1String = lineString.split(":");
    		String[] dataStrings = temp1String[1].split(",");
    		if (Integer.valueOf(dataStrings[0]) == 100) {
				resultString = dataStrings[1];
			}else if(Integer.valueOf(dataStrings[0])== 400){
				resultString = "Bad Request.";
			}else if(Integer.valueOf(dataStrings[0])== 401){
				resultString = "Unauthorized.";
			}else if (Integer.valueOf(dataStrings[0]) == 404) {
				resultString = "not found.";
			}else if (Integer.valueOf(dataStrings[0]) == 500) {
				resultString = "Server Error.";
			}else {
				resultString = "Fuck!!!!!!!!!!";
			}
    	}catch (Exception e) {
    		resultString = e.toString();
    	}
    	
		return resultString;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.