在Java中,""(空引号)和""(带有单个空格的引号)之间的区别是char什么?如何根据a测试前者?
我正在开发一个使用 HTTP GET 请求与服务器通信的 J2ME 应用程序。我已经有了一种生成 URL 编码参数的方法。
在目前的形式中,它不能满足空字符串,我见过的其他代码片段也有这个缺陷,因为它们都依赖于比较字符串参数的单个字符。我之前问过一个与这个空字符困境相关的问题
编辑:
对服务器(Play 1.0)的请求采用以下形式
http://server.com/setName/firstname/othername/lastname
参数不能为空,所以 http:server.com/setname/firstname//lastname 无效
参数是从 json 对象中检索的。目前,我拥有的 url 编码方法将对所有提取的参数进行正确编码,并保留任何无法按原样转换的字符。字符串中的空格如“Jo e”和空格字符本身将分别编码为 Jo%20e 和 %20。JSON 对象
{ "firstname":"joe"
"othername":""
"lastname":"bloggs"
}
Run Code Online (Sandbox Code Playgroud)
然而,将导致无效的 url http://server.com/setname/joe//bloggs因为 othername 参数是一个空字符串并且由我的方法保留。
我可以检查即将返回的字符串是否为空并返回一个空格字符。但我想知道是否对这种方法没有更好的修改,或者是否有一种更强大的全新方法?
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(); …Run Code Online (Sandbox Code Playgroud)