0

嗨,我正在尝试通过使用网站查找人名(20 个名字)的性别.....

首先,我以“start:234 stop:543 annotation:john”的格式提取 xml 元素。所以我正在阅读用空格分隔符拆分的特定元素的文本内容并传递给向量(NameBuffer 类是 dong 那个)。现在我将所有元素作为矢量对象。为了仅获取注释(所需名称所在的位置),我正在迭代向量 fn 并使用 fn.annotation 读取字符串,如下所示

NameBuffer fln = null;  
    String[] names=null;  
    Vector<NameBuffer> fullNameVector = Execute.readNames(new File("/abc.xml"), "fullname");//Reading xml element split and put as vector object  
    Iterator itr1 = fullNameVector.iterator();  
    while(itr1.hasNext())  
    {  
        fln=(NameBuffer) itr1.next();  
        String st1 = fln.toString();  
        NameBuffer fn= new NameBuffer(st1); //NameBuffer class constructor  takes each object in vector and split each object and returns contents like fn.start is 234,fn.stop is 543.fn.annotation is john....  

        URL url = new URL("http://www.gpeters.com/names/baby-names.php?name=fn.annotation");  
            HttpURLConnection connection = null;  
    try {  
        // Create connection  

        connection = (HttpURLConnection) url.openConnection();  
        connection.setRequestMethod("GET");  
        connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
        connection.setRequestProperty("Content-Language", "en-US");  
        connection.setUseCaches(false);  
        connection.setDoInput(true);  
        connection.setDoOutput(true);  
        connection.connect();  

        // Get Response  
        InputStream is = connection.getInputStream();  
        int status = connection.getResponseCode();  
        //System.out.println(status);  

        BufferedReader rd = new BufferedReader(new InputStreamReader(is));  
        String line;  
        while ((line = rd.readLine()) != null) {  
         if(line.contains("It's a boy!"))  
                 {  
             System.out.println(fn.annotation+"is Male");  
                 }  
         if(line.contains("It's a girl!"))  
         {  
             System.out.println(fn.annotation+"is Female");  
         }  


        }  

        rd.close();  

所以 fn.annotation 返回所有名称,但是当我将它作为参数传递给 url 时,请求没有以实际名称发送....这是语法错误还是我需要以其他方式执行?

4

2 回答 2

0

如果我正确理解您的 NameBuffer 类的工作方式,您应该这样做:

URL url = new URL("http://www.gpeters.com/names/baby-names.php?name=" + fn.annotation);
于 2013-10-21T09:03:51.630 回答
0

你的网址应该是

"http://www.gpeters.com/names/baby-names.php?name=" + fn.annotation 

我对吗 ?

于 2013-10-21T09:02:42.197 回答