This example shows how to fix the Jsoup Connection error: org.jsoup.UnsupportedMimeTypeException error while using the Jsoup.
How to fix Connection error: org.jsoup.UnsupportedMimeTypeException?
By default, Jsoup supports below given content types.
1) text/*
2) application/xml
3) application/xml+xhtml
If you try to parse any page whose content type is not one of the above content types, Jsoup throws “Connection error: org.jsoup.UnsupportedMimeTypeException” exception as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javacodeexamples.libraries.jsoup; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JsoupContentTypeExample { public static void main(String[] args) throws IOException { try{ Document document = Jsoup.connect("http://ip.jsontest.com/").get(); }catch(IOException ioe){ ioe.printStackTrace(); } } } |
Output
1 2 3 4 5 6 |
org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json; charset=ISO-8859-1, URL=http://headers.jsontest.com/ at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:547) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205) at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194) at com.javacodeexamples.libraries.jsoup.JsoupContentTypeExample.main(JsoupContentTypeExample.java:14) |
I have used the JSON test API hosted at http://ip.jsontest.com/ which gives the user’s IP address in JSON format. The content type returned by the API is “application/json” which is not supported by Jsoup by default, hence the exception “org.jsoup.UnsupportedMimeTypeException: Unhandled content type”.
How to resolve this exception?
We can use ignoreContentType
method of the Connection class to ignore the content type returned by the webpage.
1 |
Connection ignoreContentType(boolean ignore) |
Passing true in the ignoreContentType
method ignores the document’s content type while parsing the response.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javacodeexamples.libraries.jsoup; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JsoupContentTypeExample { public static void main(String[] args) throws IOException { try{ Document document = Jsoup.connect("http://ip.jsontest.com/") .ignoreContentType(true) .get(); System.out.println(document.text()); }catch(IOException ioe){ ioe.printStackTrace(); } } } |
Output
1 |
{"ip": "xxx.xxx.xxx.xxx"} |
If you want to learn more about HTML parsing using Jsoup, please visit the Jsoup tutorial with examples.
Please let me know your views in the comments section below.
respected sir
can you make a java program to search a query and returns the URL as a result like google search engine ??
please send me the code in my email : [email protected]
I can’t send you the code. But if you have a requirement, have a look at Apache Solr.