j2me访问dotnetwerbservice[分享]
post by: chinapeople @ 2003-9-21 12:51:55
1.思路:使用j2me中本身自带的httpconnection访问webservice,调用http://localhost/roadwebservice/roadws.asmx/中的方法webservicetest,参数为param。如下:
private void connect() {
httpconnection hc = null;
//inputstream in = null;
datainputstream in = null;
string s="";
string url = "http://localhost/roadwebservice/roadws.asmx/webservicetest?param="+inputtextfield.getstring();
try {
hc = (httpconnection)connector.open(url);
int ch;
in = hc.opendatainputstream();
while((ch=in.read())!=-1){
s=s+(char)ch;
}
//string s = in.readutf();
in.close();
hc.close();
mmessageitem.settext(s);
}
catch (ioexception ioe) {
mmessageitem.settext(ioe.tostring());
}
// mdisplay.setcurrent(mmainform);
string [] items;
//此处是对的到的字符串s进行xml解析。
items = parseusingkxml( s );
mdisplay.setcurrent( new itemlist( items ) );
}
这时候的到的字符串流是xml格式的,如下: hello zl
使用第三方的cldc 環境下運作的開放原始碼 xml 分析器 ── kxml,從 http://www.kxml.org/ 下載 kxml 包。对获取的xml格式的数据流进行解析,方法如下:
private string[] parseusingkxml( string xml ){
try {
bytearrayinputstream bin =
new bytearrayinputstream(
xml.getbytes() );
inputstreamreader in = new inputstreamreader( bin );
xmlparser parser = new xmlparser( in );
vector items = new vector();
parsekxmlitems( parser, items );
system.out.println(items.size());
string[] tmp = new string[ items.size() ];
items.copyinto( tmp );
return tmp;
}
catch( ioexception e ){
return new string[]{ e.tostring() };
}
}
private void parsekxmlitems( xmlparser parser, vector items )
throws ioexception {
boolean initem = false;
while( true ){
parseevent event = parser.read();
switch( event.gettype() ){
case xml.start_tag:
if( event.getname().equals( "string" ) ){
initem = true;
}
break;
case xml.end_tag:
if( event.getname().equals( "string" ) ){
initem = false;
}
break;
case xml.text:
if( initem ){
items.addelement( event.gettext() );
}
break;
case xml.end_document:
return;
}
}
}
class itemlist extends list implements commandlistener {
itemlist( string[] list ){
super( "items", implicit, list, null );
addcommand( mexitcommand );
setcommandlistener( this );
}
public void commandaction( command c, displayable d ){
if( c == mexitcommand ){
exitmidlet();
}
}
}
经过函数parsekxmlitems的解析后数据将输出至items中,并用于显示。
附录1:
dotnet中的webservice的方法webservicetest(具体的建立一个dotnetwebservie请参考其他资料):
public function webservicetest(byval param as string) as string
if param = "cqy" then
webservicetest = "hello cqy"
elseif param = "zl" then
webservicetest = "hello zl"
elseif param = "zy" then
webservicetest = "hello zy"
else
webservicetest = "who are you"
end if
end function
附录2
客户端j2me源代码:// httpmidlet.java
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import org.kxml.*;
import org.kxml.parser.*;
public class httpmidlet
extends midlet
implements commandlistener {
private display mdisplay;
private form mmainform;
private stringitem mmessageitem;
private command mexitcommand, mconnectcommand;
private final textfield inputtextfield;
public httpmidlet() {
mmainform = new form("hitmidlet");
mmessageitem = new stringitem(null, "");
mexitcommand = new command("退出", command.exit, 0);
mconnectcommand = new command("连接",
command.screen, 0);
mmainform.append(mmessageitem);
inputtextfield = new textfield("input text","", 128,textfield.any);
mmainform.append(inputtextfield);
mmainform.addcommand(mexitcommand);
mmainform.addcommand(mconnectcommand);
mmainform.setcommandlistener(this);
}
public void startapp() {
mdisplay = display.getdisplay(this);
mdisplay.setcurrent(mmainform);
}
public void exitmidlet(){
notifydestroyed();
}
public void pauseapp() {}
public void destroyapp(boolean unconditional) {}
public void commandaction(command c, displayable s) {
if (c == mexitcommand)
notifydestroyed();
else if (c == mconnectcommand) {
form waitform = new form("waiting…");
mdisplay.setcurrent(waitform);
thread t = new thread() {
public void run() {
connect();
}
};
t.start();
}
}
private void connect() {
httpconnection hc = null;
//inputstream in = null;
datainputstream in = null;
string s="";
string url = "http://localhost/roadwebservice/roadws.asmx/webservicetest?param="+inputtextfield.getstring();
try {
hc = (httpconnection)connector.open(url);
int ch;
in = hc.opendatainputstream();
while((ch=in.read())!=-1){
s=s+(char)ch;
}
//string s = in.readutf();
in.close();
hc.close();
mmessageitem.settext(s);
}
catch (ioexception ioe) {
mmessageitem.settext(ioe.tostring());
}
// mdisplay.setcurrent(mmainform);
string [] items;
items = parseusingkxml( s );
mdisplay.setcurrent( new itemlist( items ) );
}
private string[] parseusingkxml( string xml ){
try {
bytearrayinputstream bin =
new bytearrayinputstream(
xml.getbytes() );
inputstreamreader in = new inputstreamreader( bin );
xmlparser parser = new xmlparser( in );
vector items = new vector();
parsekxmlitems( parser, items );
system.out.println(items.size());
string[] tmp = new string[ items.size() ];
items.copyinto( tmp );
return tmp;
}
catch( ioexception e ){
return new string[]{ e.tostring() };
}
}
private void parsekxmlitems( xmlparser parser, vector items )
throws ioexception {
boolean initem = false;
while( true ){
parseevent event = parser.read();
switch( event.gettype() ){
case xml.start_tag:
if( event.getname().equals( "string" ) ){
initem = true;
}
break;
case xml.end_tag:
if( event.getname().equals( "string" ) ){
initem = false;
}
break;
case xml.text:
if( initem ){
items.addelement( event.gettext() );
}
break;
case xml.end_document:
return;
}
}
}
class itemlist extends list implements commandlistener {
itemlist( string[] list ){
super( "items", implicit, list, null );
addcommand( mexitcommand );
setcommandlistener( this );
}
public void commandaction( command c, displayable d ){
if( c == mexitcommand ){
exitmidlet();
}
}
}
}
用wtk2.0建立project,名称为httpmidlet,类名称为httpmidlet
将httpmidlet.java文件拷贝至wtk目录下的apps\httpmidlet的src目录下,将第三方kxml文件ksoap-midp.zip拷贝至apps\ httpmidlet\lib目录下,编译即可。
