I need some coding help. I wanted to add some code to replace spaces in a string with %20’s So I Googled it and found out I needed to use java.net.URLEncoder.encode. So prior to adding this it seemed to work fine but obviously I only got the first word of the message. Now in the designer it shows an error on both of the lines indicated. Where I’m using the .encode and on the sendHttpGetRequest() call as well. With the .encode line commented out as it shows below, the script works and the HTTP Get does work, but the log does show a ton of errors right after the logInfo line.
var String url = "https://autoremotejoaomgcd.appspot.com/sendnotification?key=12345longkeyhere&title=Notify&text="
var String encodedResponse
encodedResponse = "This%20is%20a%20test"
//encodedResponse = java.net.URLEncoder.encode(Response.state.toString, "UTF-8"); // getting red X error in the designer
url = url + encodedResponse
sendHttpGetRequest(url) // also getting red X error in designer but it works
openHAB also provides a short-cut here, in that it’s String type has an encode() method. This can simplify your code a little, as can be sen in this test-snippet:
rule "Test Encoding"
Time cron "0/3 * * * * ?"
var param = 'this is a test'
logInfo("test-encode-alt", URLEncoder::encode(param, 'UTF-8'))
This uses “+” for spaces, instead of “%20” but servers accept both (if they’re well behaved)
Multiple markers at this line
- Couldn't resolve reference to JvmIdentifiableElement 'net'.
- Couldn't resolve reference to JvmIdentifiableElement 'URLEncoder'.
- This expression is not allowed in this context, since it doesn't
cause any side effects.
- no viable alternative at input '::'
- Couldn't resolve reference to JvmIdentifiableElement 'java'.
- Couldn't resolve reference to JvmIdentifiableElement 'encode'.
I have another place where I need the %20’s instead of the +'s. The rule sends text to the command line, to run espeak, on my linux machine and the result is that it “says” “plus”. I’m guessing I need to do a simple string replace but I’m not sure of the syntax for that.
Lastly, any idea why the sendHttpGetRequest(url) (and the executeCommandLine(espeakcommand) for that matter) shows as an error in the designer and gives me a errors in the openhab.log? They both work despite all these errors? I’m guessing I need to “must fully qualify each and every reference with the complete package name” but I can’t figure out what that would be. Again it does work so maybe it’s not a big deal but it is bloating my openhab.log file.
I’m working in a script not a rule and I read that I “must fully qualify each and every reference with the complete package name” and can’t use import. I also tried your guess about where sendHttpGetRequest() lives and tired which resulted in the same designer error warning.