2016-05-12 42 views
0

我試圖下載this one 等頁面的內容,並將它寫入.txt文件供以後使用。嘗試使用jsoup將URL事件下載到.txt文件中

doc = Jsoup.connect(link).userAgent("Mozilla").get(); 
String cityInfo = doc.html();    

int index = cityInfo.indexOf("},"); // keeps just the first object as it has the highest score. 
String cityInfo1 = cityInfo.substring(index+1) + "}]}"; // gets the object in the correct format as some characters are not selected when downloading 
bw1.write(cityInfo1); //saves json object into text file 

我不斷收到此錯誤,如果我使用ignoreContentType(true)方法,它只是擺脫錯誤的和我的文本文件保持爲空。

"Exception in thread "main" 
org.jsoup.UnsupportedMimeTypeException: Unhandled content type. 
Must be text/*, application/xml, or application/xhtml+xml. 
Mimetype=application/json, URL=http://transport.opendata.ch/v1/locations?query=Aarau" 
+0

爲什麼要用Jsoup的JSON格式的數據? – Tim

+0

我想只將整個頁面作爲文本下載並稍後處理。你會建議什麼? – BlueWookie

+0

查看JSON格式:https://en.wikipedia.org/wiki/JSON – Tim

回答

0

添加ignoreContentType(true)

doc = Jsoup.connect(link).ignoreContentType(true).userAgent("Mozilla").get();

+0

我試過了,但仍然沒有寫在我的文本文件中。我得到這個:「線程中的異常」主要「org.jsoup.HttpStatusException:HTTP錯誤獲取URL狀態= 405,URL = http://transport.opendata.ch/v1/locations?query = Aarau」 – BlueWookie

+0

啊,那就是相反的--405表明服務器不明白你正在提供什麼,或者不願意返回它認爲你所要求的類型。解決方案是針對您提供和請求的內容類型更具體。 – Lee

+0

所以我應該下載它只是文本,JSON或其他格式類型? – BlueWookie

相關問題