JSON and url Encoding in Codename one

90 views
Skip to first unread message

Gordons

unread,
Jan 9, 2013, 10:54:13 AM1/9/13
to codenameone...@googlegroups.com
I am trying to make an HTTP request but the parameter accompanying the url has to be JSON encoded and then URL encoded.
For clarification purposes, I am trying to achieve something like this:

String query = URLEncoder.encode((key1 = value1, key2 = value2).json_encode);
String url = "http//the_url?where="+query;

Please how can I achieve this

Eric Coolman

unread,
Jan 9, 2013, 11:35:24 AM1/9/13
to codenameone...@googlegroups.com
        Hashtable data = new Hashtable();
        data.put("key1", "value1");
        data.put("key2", "value2");
        String json = Result.fromContent(data).toString();
        System.out.println(json);
        URI uri = new URI("http", "codenameone.com", "/", "where="+json, null);
        System.out.println(uri.toString());

Above will print out:

{
  "key2": value2,
  "key1": value1
}
http://codenameone.com/?where=%7B%A%20%20%22key2%22:%20value2,%A%20%20%22key1%22:%20value1%A%7D

Shai Almog

unread,
Jan 9, 2013, 11:39:37 AM1/9/13
to codenameone...@googlegroups.com
Won't it be simpler to just add the JSON string to the connection request since it already encodes the data?
conRequest.addArgument("where", json);

Eric Coolman

unread,
Jan 9, 2013, 11:59:11 AM1/9/13
to codenameone...@googlegroups.com
Yes it would :)  My bad

Agada Godwin

unread,
Jan 9, 2013, 12:36:25 PM1/9/13
to codenameone...@googlegroups.com

Thank you very much, it has worked.

sent from my Android device

--
--
You received this message because you are subscribed to the Google
Groups "CodenameOne Discussions" group.
 
For more information about Codename One please visit http://www.codenameone.com/
 
To post to this group, send email to
codenameone...@googlegroups.com
To unsubscribe from this group, send email to
codenameone-discu...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/codenameone-discussions?hl=en?hl=en
 
 
 
Reply all
Reply to author
Forward
0 new messages