|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一旦你有了思想,那你编的程序就有了灵魂,不管是什么语言到了你的手里都会是你的工具而已,他们的价值是能尽快帮助你实现你想要的目标。但是如果你没有了思想,那就像是海里的帆船失去了船帆,是很难到打海的另一边的。会见J2me会见dotnetwerbservice[分享]
Postby:chinapeople@2003-9-2112:51:55
1.思绪:利用j2me中自己自带的HttpConnection会见webservice,挪用http://localhost/RoadWebService/RoadWS.asmx/中的办法WebServiceTest,参数为param。以下:
privatevoidconnect(){
HttpConnectionhc=null;
//InputStreamin=null;
DataInputStreamin=null;
Strings="";
Stringurl="http://localhost/RoadWebService/RoadWS.asmx/WebServiceTest?param="+inputTextField.getString();
try{
hc=(HttpConnection)Connector.open(url);
intch;
in=hc.openDataInputStream();
while((ch=in.read())!=-1){
s=s+(char)ch;
}
//Strings=in.readUTF();
in.close();
hc.close();
mMessageItem.setText(s);
}
catch(IOExceptionioe){
mMessageItem.setText(ioe.toString());
}
//mDisplay.setCurrent(mMainform);
String[]items;
//此处是对的到的字符串S举行XML剖析。
items=parseUsingkXML(s);
mDisplay.setCurrent(newItemList(items));
}
这时候候的到的字符串流是XML格局的,以下:hellozl
利用第三方的CLDCh境下作的_放原始aXML剖析器──kXML,http://www.kxml.org/下dkXML包。对猎取的XML格局的数据流举行剖析,办法以下:
privateString[]parseUsingkXML(Stringxml){
try{
ByteArrayInputStreambin=
newByteArrayInputStream(
xml.getBytes());
InputStreamReaderin=newInputStreamReader(bin);
XmlParserparser=newXmlParser(in);
Vectoritems=newVector();
parsekXMLItems(parser,items);
System.out.println(items.size());
String[]tmp=newString[items.size()];
items.copyInto(tmp);
returntmp;
}
catch(IOExceptione){
returnnewString[]{e.toString()};
}
}
privatevoidparsekXMLItems(XmlParserparser,Vectoritems)
throwsIOException{
booleaninItem=false;
while(true){
ParseEventevent=parser.read();
switch(event.getType()){
caseXml.START_TAG:
if(event.getName().equals("string")){
inItem=true;
}
break;
caseXml.END_TAG:
if(event.getName().equals("string")){
inItem=false;
}
break;
caseXml.TEXT:
if(inItem){
items.addElement(event.getText());
}
break;
caseXml.END_DOCUMENT:
return;
}
}
}
classItemListextendsListimplementsCommandListener{
ItemList(String[]list){
super("Items",IMPLICIT,list,null);
addCommand(mExitCommand);
setCommandListener(this);
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==mExitCommand){
exitMIDlet();
}
}
}
经由函数parsekXMLItems的剖析后数据将输入至Items中,并用于显现。
附录1:
dotnet中的webservice的办法webservicetest(详细的创建一个dotnetwebservie请参考其他材料):
PublicFunctionWebServiceTest(ByValparamAsString)AsString
Ifparam="cqy"Then
WebServiceTest="hellocqy"
ElseIfparam="zl"Then
WebServiceTest="hellozl"
ElseIfparam="zy"Then
WebServiceTest="hellozy"
Else
WebServiceTest="whoareyou"
EndIf
EndFunction
附录2
客户端j2me源代码://HttpMidlet.java
importjava.io.*;
importjava.util.*;
importjavax.microedition.io.*;
importjavax.microedition.lcdui.*;
importjavax.microedition.midlet.*;
importorg.kxml.*;
importorg.kxml.parser.*;
publicclassHttpMidlet
extendsMIDlet
implementsCommandListener{
privateDisplaymDisplay;
privateFormmMainform;
privateStringItemmMessageItem;
privateCommandmExitCommand,mConnectCommand;
privatefinalTextFieldinputTextField;
publicHttpMidlet(){
mMainform=newForm("HitMIDlet");
mMessageItem=newStringItem(null,"");
mExitCommand=newCommand("加入",Command.EXIT,0);
mConnectCommand=newCommand("毗连",
Command.SCREEN,0);
mMainform.append(mMessageItem);
inputTextField=newTextField("Inputtext","",128,TextField.ANY);
mMainform.append(inputTextField);
mMainform.addCommand(mExitCommand);
mMainform.addCommand(mConnectCommand);
mMainform.setCommandListener(this);
}
publicvoidstartApp(){
mDisplay=Display.getDisplay(this);
mDisplay.setCurrent(mMainform);
}
publicvoidexitMIDlet(){
notifyDestroyed();
}
publicvoidpauseApp(){}
publicvoiddestroyApp(booleanunconditional){}
publicvoidcommandAction(Commandc,Displayables){
if(c==mExitCommand)
notifyDestroyed();
elseif(c==mConnectCommand){
Formwaitform=newForm("Waiting...");
mDisplay.setCurrent(waitform);
Threadt=newThread(){
publicvoidrun(){
connect();
}
};
t.start();
}
}
privatevoidconnect(){
HttpConnectionhc=null;
//InputStreamin=null;
DataInputStreamin=null;
Strings="";
Stringurl="http://localhost/RoadWebService/RoadWS.asmx/WebServiceTest?param="+inputTextField.getString();
try{
hc=(HttpConnection)Connector.open(url);
intch;
in=hc.openDataInputStream();
while((ch=in.read())!=-1){
s=s+(char)ch;
}
//Strings=in.readUTF();
in.close();
hc.close();
mMessageItem.setText(s);
}
catch(IOExceptionioe){
mMessageItem.setText(ioe.toString());
}
//mDisplay.setCurrent(mMainform);
String[]items;
items=parseUsingkXML(s);
mDisplay.setCurrent(newItemList(items));
}
privateString[]parseUsingkXML(Stringxml){
try{
ByteArrayInputStreambin=
newByteArrayInputStream(
xml.getBytes());
InputStreamReaderin=newInputStreamReader(bin);
XmlParserparser=newXmlParser(in);
Vectoritems=newVector();
parsekXMLItems(parser,items);
System.out.println(items.size());
String[]tmp=newString[items.size()];
items.copyInto(tmp);
returntmp;
}
catch(IOExceptione){
returnnewString[]{e.toString()};
}
}
privatevoidparsekXMLItems(XmlParserparser,Vectoritems)
throwsIOException{
booleaninItem=false;
while(true){
ParseEventevent=parser.read();
switch(event.getType()){
caseXml.START_TAG:
if(event.getName().equals("string")){
inItem=true;
}
break;
caseXml.END_TAG:
if(event.getName().equals("string")){
inItem=false;
}
break;
caseXml.TEXT:
if(inItem){
items.addElement(event.getText());
}
break;
caseXml.END_DOCUMENT:
return;
}
}
}
classItemListextendsListimplementsCommandListener{
ItemList(String[]list){
super("Items",IMPLICIT,list,null);
addCommand(mExitCommand);
setCommandListener(this);
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==mExitCommand){
exitMIDlet();
}
}
}
}
用wtk2.0创建project,称号为HttpMidlet,类称号为HttpMidlet
将HttpMidlet.java文件拷贝至wtk目次下的appsHttpMidlet的src目次下,将第三方kxml文件ksoap-midp.zip拷贝至appsHttpMidletlib目次下,编译便可。
他们对jsp,servlet,javabean进行封装就是为了展示他们的某个思想,与java的开发并没有必然的关系,也不见得在所以情况下,别人使用起来会简单。 |
|