About the Chinese characters in the URL request

14 views
Skip to first unread message

green...@gmail.com

unread,
Apr 3, 2019, 8:03:49 AM4/3/19
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

String nickName="网名";
String url =“http://localhost:8080/updateUser/+userID+"/"+nickName+"/"+email+”/“;


1) Solution one:
Response<Map> jsonData = Rest.get(url).contentType("charset=utf-8").acceptJson().getAsJsonMap();

2) Solution two:
ConnectionRequest request = new ConnectionRequest();
            request.setPost(false);
            request.setUrl(url);
            request.addRequestHeader("contentType", "application/json; charset=utf-8");
            request.setReadRequest(true);
            NetworkManager.getInstance().addToQueueAndWait(request);

Both solutions couldn't encode Chinese into UTF-8 format by using solution one and two, even I put the charset=utf-8.

Thomas

unread,
Apr 3, 2019, 8:56:13 AM4/3/19
to CodenameOne Discussions
This is because it isn't the request content but URL get parametters
So you should probably do something like:

String nickName="网名";
String utf8nickName = new String(nickName.getBytes(), "UTF-8"); 
String url =“http://localhost:8080/updateUser/+userID+"/"+utf8nickName +"/"+email+”/“; 

Shai Almog

unread,
Apr 4, 2019, 12:31:24 AM4/4/19
to CodenameOne Discussions
Almost, this should be url encoded with % notation:
String utf8nickName = Util.encodeUrl(nickName);

This should also be done for the email as the @ character should be %40: Util.encodeUrl(email).

green...@gmail.com

unread,
Apr 4, 2019, 1:13:34 AM4/4/19
to CodenameOne Discussions
Thanks for replying, but I received error message return after encode with UTF-8.


java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:474)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Shai Almog

unread,
Apr 4, 2019, 11:46:21 PM4/4/19
to CodenameOne Discussions
Did you use the methods I mentioned?
You don't need to UTF encode.

green...@gmail.com

unread,
Apr 6, 2019, 4:14:45 PM4/6/19
to CodenameOne Discussions
I did use the method which you gave, but receive the error. And the solution which given by Shai is working.

 The problem is solved by  using "String utf8nickName = Util.encodeUrl(nickName);".

Thanks!
Reply all
Reply to author
Forward
0 new messages