|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
但是一些大型开发或者是保密型比较高的项目都会用java,原因有2点,一:java是开源的,不怕别人留后门,偷我工具,.net就不一样了,保持微软的一向风格,源代码不公开encode 因为MIDP没有J2SE对应的java.net.URLEncoder类,因而,要向服务器发送HTTP哀求,必需本人举行URL编码,参考JDK1.4.2的src代码,将其改成一个能用在MIDP情况中的URLEncoder类:
//URLEncoder.java
packagecom.mboker.blog.midp.http;
importjava.io.*;
/**
*Encodeurl,justlikejava.net.URLEncoder.encode()inJ2SE.<br>
*NOTE:Thisclassismodifiedfromjava.net.URLEncoderclassinJ2SE1.4.
*
*@authorXuefeng
*/
classURLEncoder{
privatestaticfinalintMAX_BYTES_PER_CHAR=10;//ratherarbitrarylimit,butsafefornow
privatestaticboolean[]dontNeedEncoding;
privatestaticfinalintcaseDiff=(a-A);
static{
dontNeedEncoding=newboolean[256];
for(inti=a;i<=z;i++){
dontNeedEncoding[i]=true;
}
for(inti=A;i<=Z;i++){
dontNeedEncoding[i]=true;
}
for(inti=0;i<=9;i++){
dontNeedEncoding[i]=true;
}
dontNeedEncoding[]=true;
dontNeedEncoding[-]=true;
dontNeedEncoding[_]=true;
dontNeedEncoding[.]=true;
dontNeedEncoding[*]=true;
}
privateURLEncoder(){}
publicstaticStringencode(Strings){
booleanwroteUnencodedChar=false;
StringBufferout=newStringBuffer(s.length());
ByteArrayOutputStreambuf=newByteArrayOutputStream(MAX_BYTES_PER_CHAR);
OutputStreamWriterwriter=newOutputStreamWriter(buf);
for(inti=0;i<s.length();i++){
intc=(int)s.charAt(i);
if(c<256&&dontNeedEncoding[c]){
out.append((char)(c==?+:c));
wroteUnencodedChar=true;
}else{
//converttoexternalencodingbeforehexconversion
try{
if(wroteUnencodedChar){//Fixfor4407610
writer=newOutputStreamWriter(buf);
wroteUnencodedChar=false;
}
writer.write(c);
/*
*IfthischaracterrepresentsthestartofaUnicode
*surrogatepair,thenpassintwocharacters.Itsnot
*clearwhatshouldbedoneifabytesreservedinthe
*surrogatepairsrangeoccursoutsideofalegalsurrogate
*pair.Fornow,justtreatitasifitwereanyother
*character.
*/
if(c>=0xD800&&c<=0xDBFF){
if((i+1)<s.length()){
intd=(int)s.charAt(i+1);
if(d>=0xDC00&&d<=0xDFFF){
writer.write(d);
i++;
}
}
}
writer.flush();
}catch(IOExceptione){
buf.reset();
continue;
}
byte[]ba=buf.toByteArray();
for(intj=0;j<ba.length;j++){
out.append(%);
charch=toHex((ba[j]>>4)&0xF);
//convertingtouseuppercaseletteraspartof
//thehexvalueifchisaletter.
if(isLetter(ch)){
ch-=caseDiff;
}
out.append(ch);
ch=toHex(ba[j]&0xF);
if(isLetter(ch)){
ch-=caseDiff;
}
out.append(ch);
}
buf.reset();
}
}
returnout.toString();
}
privatestaticchartoHex(intdigit){
if((digit>=16)||(digit<0)){
return ;
}
if(digit<10){
return(char)(0+digit);
}
return(char)(a-10+digit);
}
privatestaticbooleanisLetter(charc){
return(c>=a&&c<=z);
}
}
J2SE的URLEncoder依附于java.lang.Character的很多特征,将其全体剔除并改革为基础运算,便可在MIDP情况中利用,中文测试也统统一般。
IDE是好。java中的IDE更是百花齐放,你用jbuilder能说jbuilder赶不上vs吗?用eclipse,netbeans也很舒服啊。我就不明白“稍微差一些”那一些是从哪里差来的。 |
|