2012-05-23 68 views
0

我可以在沒有cURL問題的情況下拉取用戶的狀態,但是當我用Java連接時,xml會被截斷,我的解析器會哭泣。我正在測試小用戶,所以它不是嗆數據或任何東西。用java連接失敗的Twitter請求

public void getRuserHx(){ 
System.out.println("Getting user status history..."); 

String https_url = "https://twitter.com/statuses/user_timeline/" + idS.rootUser + ".xml?count=100&page=[1-32]"; 
URL url; 
try {  
    url = new URL(https_url); 
    HttpsURLConnection con = (HttpsURLConnection)url.openConnection(); 
    con.setRequestMethod("GET"); 
    con.setReadTimeout(15*1000); 

    //dump all the content into an xml file 
    print_content(con); 

} 
catch (MalformedURLException e) { 
    e.printStackTrace(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
} 

System.out.println("Finished downloading user status history."); 

}

private void print_content(HttpsURLConnection con){ 
    if(con!=null){ 

    try {   
     BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); 

      File userHx = new File("/" + idS.rootUser + "Hx.xml"); 
      PrintWriter out = new PrintWriter(idS.hoopoeData + userHx); 

      String input;   
      while ((input = br.readLine()) != null){ 
      out.println(input); 
      } 

     br.close(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

該請求不需要身份驗證。對不起我的醜陋代碼。我的教授說輸入不重要,所以我的I/O是一個火車殘骸。

+0

你確定你的連接是正確的,併爲你的程序得到了輸入嗎? –

+0

是的,xml正在寫出來,但每次在同一行中該文件被截斷(中間標記)。如果我使用命令行中的cURL,該文件會很好。看起來,與Java的連接不會持久。 –

+0

如果刪除設置讀取超時值會發生什麼情況? –

回答

1

當你寫出內容時,你必須刷新輸出流。您是否刷新或關閉輸出流?

+0

我添加了打印方法的代碼。我確實關閉了BufferedReader,那是什麼意思? –

+0

我加了out.flush(),現在我想到了。謝謝。 –