答案: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
上一个:J2ME HTTP POST
下一个:Java手机程序设计入门 电子书开放下载(转自CSDN)