·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » J2ME访问DOTNETWERBSERVICE

J2ME访问DOTNETWERBSERVICE

类别: JSP教程  评论数:0 总得分:0
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 ?h境下咦鞯拈_放原始瘁 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目录下的appsHttpMidlet的src目录下,将第三方kxml文件ksoap-midp.zip拷贝至apps HttpMidletlib目录下,编译即可。
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1