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目錄下,編譯即可。