Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I cater for empty strings while encoding parameters for a GET request?
    text
    copied!<p>I'm working on a J2ME application that uses HTTP GET requests to communicate with a server. I already have a method to generate URL encoded parameters.</p> <p>In its present form it <em>can not</em> cater for empty strings,other code snippets I've seen have this deficiency as well, because they all rely on comparing the individual characters of a string argument.<a href="https://stackoverflow.com/q/12060061">I have previously asked a question related to this empty char dilemma</a></p> <p><strong>EDIT:</strong></p> <p>Requests to the server (Play 1.0) are in the form </p> <p><a href="http://server.com/setName/firstname/othername/lastname" rel="nofollow noreferrer">http://server.com/setName/firstname/othername/lastname</a></p> <p>Parameters cannot be null so http:server.com/setname/firstname//lastname is invalid</p> <p>The parameters are retrieved from a json object.Currently the url encoding method I have will encode all the extracted parameters correctly and leave any character which cannot be converted as is.A space in a string as in "Jo e" and space character itself will be encoded as Jo%20e and %20 respectively. The JSON object</p> <pre><code>{ "firstname":"joe" "othername":"" "lastname":"bloggs" } </code></pre> <p>will however result in the invalid url <a href="http://server.com/setname/joe//bloggs" rel="nofollow noreferrer">http://server.com/setname/joe//bloggs</a> because the othername parameter is an empty string and is left as such by my method.</p> <p>I could check that the string about to be returned is empty and return a space character instead. But I'm wondering if there isn't a better modification to this method or a whole new approach which would be more robust?</p> <pre><code> public static String urlEncode(String s) { ByteArrayOutputStream bOut = null; DataOutputStream dOut = null; ByteArrayInputStream bIn = null; StringBuffer ret = new StringBuffer(); try { bOut=new ByteArrayOutputStream(); dOut = new DataOutputStream(bOut); //return value dOut.writeUTF(s); bIn = new ByteArrayInputStream(bOut.toByteArray()); bIn.read(); bIn.read(); int c = bIn.read(); while (c &gt;= 0) { if ((c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') || (c &gt;= '0' &amp;&amp; c &lt;= '9') || c == '.' || c == '-' || c == '*' || c == '_') { ret.append((char) c); } else if (c == ' ' ) { //ret.append('+'); ret.append("%20"); } else { if (c &lt; 128) { appendHex(c, ret); } else if (c &lt; 224) { appendHex(c, ret); appendHex(bIn.read(), ret); } else if (c &lt; 240) { appendHex(c, ret); appendHex(bIn.read(), ret); appendHex(bIn.read(), ret); } } c = bIn.read(); } } catch (IOException ioe) { System.out.println("urlEncode:"+ioe); return s; } return ret.toString(); } private static void appendHex(int arg0, StringBuffer buff) { buff.append('%'); if (arg0 &lt; 16) { buff.append('0'); } buff.append(Integer.toHexString(arg0)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload