• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Url encoding in GET request

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my code, sending a request to an http server, I've to add some params/values via GET method, but I noticed that all will works only by formating my http request (for example replacing 'space' with '%20'). My question: Exists an UrlEncoding method? Have I to define any HttpConnection's property.
Thanx in advance.
Marco
HttpConnection http = null;
InputStream iStr = null;
String str = null;
boolean ret = false;
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
if (c == cmdTest) {
if (txt.getString() != null)
System.out.print(txt.getString());
try {
String formatQuery= txt.getString();
http = (HttpConnection) Connector.open(
"http://XXXXXXX/J2meServer/nonQuery.aspx?sql=" +
formatQuery(txt.getString()));
iStr = http.openInputStream();
String msg=new String("");
msg=processServerResponse(http, iStr);
private String formatQuery (String oldQuery)
{
String newQuery=new String(oldQuery.trim());
while (newQuery.indexOf(' ')!=-1)
{
int i=newQuery.indexOf(' ');
System.out.print("\n"+newQuery);
newQuery=newQuery.substring(0,i)+"%20"+newQuery.substring(i+1, newQuery.length());
}
return newQuery;
}
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to encode URLs by hand in MIDP.
 
Marco Battaglia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Yuan, I'm reading your book, bought in Italy.
 
Michael Yuan
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool!
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know of a custom-made class that handles this? I was hoping there was something that already existed for the URL encoding of GET params. If someone has implemented a good system for this and wouldn't mind sharing their code, let me know.
Cheers,
Greg
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private String urlEncoder(String s) {
try {
if (s == null) {
return (s);
}
StringBuffer sb = new StringBuffer(100);
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z')) {
sb.append(c);
continue;
}
if (c > 15) { // is it a non-control char, ie. >x0F so 2 chars
sb.append("%" + Integer.toHexString( (int) c)); // just add % and the string
} else {
sb.append("%0" + Integer.toHexString( (int) c)) ; // otherwise need to add a leading 0
}
}
}
return (sb.toString());
} catch (Exception ex) {
System.out.println("Exception, URLencode string is " + s);
return (null);
}
}
 
Greg Schwartz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sharing that code Roy. Looks like a solid solution!
 
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the code.
 
I'm full of tinier men! And a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic