Help with script coding syntax

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
logInfo("autoremote", url)


sendHttpGetRequest(url)  // also getting red X error in designer but it works


For Java static methods, you use :: between the Class name and the static method name, like this:

java.net.URLEncoder::encode(Response.state.toString, 'UTF-8')

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:

import java.net.URLEncoder

rule "Test Encoding"
when
	Time cron "0/3 * * * * ?"
then
        var param = 'this is a test'
	logInfo("test-encode", param.encode('UTF-8')) 
	logInfo("test-encode-alt", URLEncoder::encode(param, 'UTF-8')) 
end

This uses “+” for spaces, instead of “%20” but servers accept both (if they’re well behaved)

3 Likes

Ok so here are the results.

Using the openHAB short-cut, .encode(‘UTF-8’) worked in my original sample.

This did not:

java.net.URLEncoder::encode(Response.state.toString, 'UTF-8')

The designer shows this error…

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.

var String espeakcommand = "espeak -v en-north -p 25 -s 100 " + Response.state.toString.encode("UTF-8")

executeCommandLine(espeakcommand)

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.

Solved my issue with the command line for espeak. I just found another short-cut for strings in openhab stringVar.replace(“this”,“that”)

Now just wondering about the errors showing for sendHttpGetRequest() and executeCommandLine().

I modified the example above to show both forms of encoding. For the URLEncoder case, you need to have the import section. This isn’t needed with String.encode(...).

I’d have to see what the specific error is. I only use CLI, so don’t have the Designer in my environment.

One “guess” would be that the Designer may not be aware of the default imports. You could test that theory by adding the following import to the top of your Rule:

import org.openhab.io.net.actions.HTTP

That’s where static public String sendHttpGetRequest(String url) lives, for example, but it’s a wild guess since I have no way of validating it.

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.

org.openhab.io.net.actions.HTTP.sendHttpGetRequest(url) 

I did find this link that indicates it is automatically statically imported into rules and scripts so it must just be a designer issue that is not recognizing it.

BTW I very much appreciate you help with this I learned a lot from it.