Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Url encoding in GET request  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanx Yuan, I'm reading your book, bought in Italy.
 
Michael Yuan
author
Ranch Hand
Posts: 1427
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks for the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!