URL error (MalformedURLException)

Hello,
I am new to Processing and also to Java (though I’ve coded a lot in other languages). I am trying to read some text from a web page for my sketch, so I tried to follow the instructions in this link, and do something like:
URL rurl = new URL("http://www.oracle.com/");
However this is not working, and I keep getting MalformedURLException, regardless of the url that I use (including the one in the Oracle example, or “http://example.com”).

The error apparently occurs before the code is executed, seemingly because the IDE tries to execute the command. This seems weird and also makes it difficult to debug. (Again I am a Java novice, maybe that’s how it is in Java-land.)

I hope some Java or URL experts who can shed light on this problem… Thanks in advance!

hi,

this example from oracle works, but when you call “http://www.oracle.com/” it s redirected to “https://www.oracle.com/” this redirection is not handled by this code
if you change to https in you code it will work:

import java.net.*;
import java.io.*;
void setup() {
  try {
    URL oracle = new URL("https://www.oracle.com/");
    BufferedReader in = new BufferedReader(
      new InputStreamReader(oracle.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
      println(inputLine);
    in.close();
  }
  catch(Exception e) {
    println(e);
  }
}

however, you can use the “loadStrings” from processing too, code will be more simple:

void setup() {
  String[] html=        loadStrings("https://www.oracle.com/");
  println(html);
}
2 Likes

Thanks!

I am still not clear why this was breaking (it worked in java outside of processing). But I’ll just use loadStrings!