2016-05-12 3 views
0

this one のようなページのコンテンツをダウンロードし、後で使用するために.txtファイルに書き込もうとしています。jsoupを使用して.txtファイルにURL contetntをダウンロードしようとしています

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

? – Tim

+0

ページ全体をテキストとしてのみダウンロードし、後で処理したい。何をお勧めしますか? – BlueWookie

+0

JSONフォーマットをチェックしてください:https://en.wikipedia.org/wiki/JSON – Tim

答えて

0

ignoreContentType(true)を追加します。

JSON形式のデータのためのJsoupを使用する理由

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

+0

私は試みましたが、何も私のテキストファイルに書き込まれません。私はこれを代わりに取得: "スレッドでの例外"メイン "org.jsoup.HttpStatusException:URLを取得するHTTPエラーステータス= 405、URL = http://transport.opendata.ch/v1/locations?query = Aarau" – BlueWookie

+0

ああ、それは逆です - 405は、サーバーがあなたが提供しているものを理解していないか、またはあなたが求めていると思うタイプを返そうとしていないことを示しています。解決策は、あなたが提供し、要求するコンテンツタイプをより具体的にすることです。 – Lee

+0

テキスト、JSON、または別のフォーマットタイプとしてダウンロードする必要がありますか? – BlueWookie

関連する問題