0

I am working on a project with jsoup. The point is that I am not able to connect to the target URL. I get this error:

W/System.err: java.net.MalformedURLException: unknown protocol: location

I don't know exactly what unknown protocol: location means.

This is my code. The code works perfectly with other websites, but there is one website where it doesn't work.

    public class connect extends AsyncTask<Void, Void, Void> {
        String string;

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                Document document = Jsoup.connect(url).get();
                Elements elements = document.select("div.tray_name");
                string = elements.html();

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

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            textView.setText(string);
        }
    }
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
Dev Eloper
  • 43
  • 1
  • 6

1 Answers1

1

You can only create a java.net.URL with a known protocol.

It appears the you are attempting to create a URL of the form:

location:some_format_dependent_on_the_meaning_of_location

Here's a self contained example to show MalformedURLException being thrown:

interface Code {
    static void main(String[] args) throws Exception {
        var url = new java.net.URL("location://1.1.1.1");

    }
}

Which displays:

Exception in thread "main" java.net.MalformedURLException: unknown protocol: location
    at java.base/java.net.URL.<init>(URL.java:674)
    at java.base/java.net.URL.<init>(URL.java:563)
    at java.base/java.net.URL.<init>(URL.java:510)
    at Code.main(Code.java:3)

You can replace the list of known protocols using the process wide URL.setURLStreamHandlerFactory or for an individiual URL using the URL(URL context, String spec, URLStreamHandler handler) constructor. (Both require security permissions if you are running under a reasonable SecurityManager.)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • Thank you for your response. It is just a regular URL format: h ttps://www.my-site.com/ So I still don't know what location means. – Dev Eloper Jan 05 '20 at 14:39
  • @DevEloper Somewhere (pointed to by the stack trace) you will have sent a `String` starting with `location:` to the constructor of a `java.net.URL`. That's what the exception message means. – Tom Hawtin - tackline Jan 05 '20 at 18:12