2013-10-21 42 views
0

HI我試圖找到人firstnames(20名)通過使用網站性別.....構建URL - Java的

首先我正在提取XML元素格式「start:234 stop:543 annotation:john」。所以我正在閱讀特定元素的空間分隔符的文本內容並傳遞給vector(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返回所有的名字,但讀字符串時,我把它作爲參數網址該請求沒有發送實際的名稱.... 這是一個語法錯誤還是我需要以另一種方式做?

回答

0

您的網址應該是

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

我說得對不對?

+0

Yes.It工作now.Thanks – user2814979

+0

它總是很高興提供幫助。 – Julien

0

如果我理解正確的NameBuffer類的工作方式,你應該這樣做:

URL url = new URL("http://www.gpeters.com/names/baby-names.php?name=" + fn.annotation); 
+0

它正在工作。謝謝 – user2814979