views:

2139

answers:

7

I'm trying to extract the query's name-value pairs from a URL using J2ME, but it doesn't make it easy. J2ME doesn't have the java.net.URL class nor does String have a split method.

Is there a way to extract name-value pairs from a URL using J2ME? Any open source implementations would be welcome too.

A: 

A URL Encoder/Decoder is really simple and easy to write. You can also look up any open source HTML to WML transcoder code on the internet and modify it. Shouldnt be too hard.

omermuhammed
A: 

Off the top of my head, it'd go something like this (warning: untested):

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, like name1=value1;name2=value2;..., although I've never seen it done in practice. If that's a concern for you, I'm sure you can fix up the code for it.

David Zaslavsky
I think there's a minor problem, when you iterate through the loop, won't the name and value strings get overwritten?
kchau
Sorry, I missed the comment you have in there.
kchau
+1  A: 

Here's my stab at it, some similarity to David's answer.

    String url = "http://www.stackoverflow.com?name1=value1&amp;name2=value2&amp;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        // o.w. 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 it helps, good luck.

kchau
+2  A: 

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

    String url = "http://www.so.com?name1=value1&amp;name2=value2&amp;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));
    }
Mark Robinson
A: 

There's a J2ME implementation that doesn't have java.net.URL? It's part of the Connected Device Configuration, Foundation Profile, Personal Basis Profile, and Personal Profile...

Edit: For the record, these are the CDC 1.1.2 links, but according to JSR36, CDC 1.0 also has a java.net.URL class.

R. Bemrose
I may be wrong, but I don't think CDC 1.1.2 is to be found much in the wild. CDC 1.1 lacks those JSRs, AFAICT.
izb
According to JSR 36 (http://jcp.org/aboutJava/communityprocess/mrel/jsr036/), CDC 1.0 also has a java.net.URL class.
R. Bemrose
CDC is not wide spread, most modern J2ME implementations only support CLDC 1.1, with a lot of CLDC 1.0 phones still out there too.
roryf
Ah, OK. I would have thought manufacturers would avoid using something with Limited in its name, but I'm not a manufacturer.
R. Bemrose
A: 
Pavel Alexeev
A: 

Since the Java JDK is open-source, you could also borrow the java URL class from the main JDK and add it to your project. This would let you use the same implementation from Java SE:

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

David