How to parse a URL in J2ME

I am trying to retrieve request name-value pairs from a URL using J2ME, but this does not simplify. J2ME does not have a java.net.URL class, and String does not have a split method.

Is there a way to extract name-value pairs from a URL using J2ME? Any open source versions are also welcome.

+4
source share
7 answers

I like kchau , but I just changed the data structure from two arrays to one Hashtable. It will also help if the number of URL parameters is unknown.

String url = "http://www.so.com?name1=value1&name2=value2&name3=value3"; Hashtable values = new Hashtable(); int s = url.indexOf("?"); int e = 0; while (s != -1) { e = url.indexOf("=", s); String name = url.substring(s + 1, e); s = e + 1; e = url.indexOf("&", s); if (e < 0) { values.put(name, url.substring(s, url.length())); } else { values.put(name, url.substring(s, e)); } s = e; } for (Enumeration num = values.keys(); num.hasMoreElements();) { String key = (String)num.nextElement(); System.out.println(key + " " + values.get(key)); } 
+4
source

Here is my blow to him, some resemblance to David.

  String url = "http://www.stackoverflow.com?name1=value1&name2=value2&name3=value3"; String[] names = new String[10]; String[] values = new String[10]; int s = url.indexOf("?"); // Get start index of first name int e = 0, idx = 0; while (s != -1) { e = url.indexOf("=", s); // Get end index of name string names[idx] = url.substring(s+1, e); s = e + 1; // Get start index of value string e = url.indexOf("&", s); // Get index of next pair if (e < 0) // Last pair values[idx] = url.substring(s, url.length()); else // ow keep storing values[idx] = url.substring(s, e); s = e; idx++; } for(int x = 0; x < 10; x++) System.out.println(names[x] +" = "+ values[x]); 

Tested it, and I think it works. Hope this helps, good luck.

+2
source

Since the Java JDK is open source, you can also take the java URL class from the main JDK and add it to your project. This will allow you to use the same implementation from Java SE:

http://www.docjar.com/html/api/java/net/URL.java.html

+2
source

On top of my head, it will look like this (warning: unchecked):

 String url = ...; int s = url.indexOf("?") + 1; while (s > 0) { int e = url.indexOf("=", s); String name = url.substring(s, e), value; s = e + 1; e = url.indexOf("&", s); if (e < 0) value = url.substring(s, e); else value = url.substring(s, e); // process name, value s = e; } 

Query strings can technically be separated by a semicolon instead of an ampersand, for example name1=value1;name2=value2;... although I have never seen this done in practice. If this bothers you, I'm sure you can fix the code for it.

+1
source

Is there a J2ME implementation that does not have java.net.URL? This is part of the Connected Device Configuration , Profile Profile , Personal Profile Profile and Personal Profile ...

Edit: for recording, these are CDC 1.1.2 links, but according to JSR36 , CDC 1.0 also has java. net.URL class.

+1
source

Also note that url parameters are encoded at the URL, so you may need to decode them first (how to do this is another question)

I get the parameters this way:

 public String getUrlParam(String url, String param) { int startIndex = url.indexOf(""+param+"="); if (startIndex == -1) return null; int length = (""+param+"=").length(); int endIndex = url.indexOf("&", startIndex+length); if (endIndex == -1) endIndex = url.length(); return URLDecode(url.substring(startIndex+length, endIndex)); }
public String getUrlParam(String url, String param) { int startIndex = url.indexOf(""+param+"="); if (startIndex == -1) return null; int length = (""+param+"=").length(); int endIndex = url.indexOf("&", startIndex+length); if (endIndex == -1) endIndex = url.length(); return URLDecode(url.substring(startIndex+length, endIndex)); } 
+1
source

The URL encoder / decoder is really simple and easy to write. You can also find any open source HTML code for the WML transcoder code on the Internet and modify it. It should not be too complicated.

-1
source

All Articles