加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

j2m2访问dotnet Webservice--以前写过,被到处转载,绝对原版

发布时间:2020-12-17 02:57:23 所属栏目:安全 来源:网络整理
导读:1.思路:使用j2me中本身自带的HttpConnection访问webservice,调用http://localhost/RoadWebService/RoadWS.asmx/中的方法WebServiceTest,参数为param。如下: private void connect() { HttpConnection hc = null; //InputStream in = null; DataInputStre
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,Displayable d ){ if( c == mExitCommand ){ exitMIDlet(); } } } } 用wtk2.0建立project,名称为HttpMidlet,类名称为HttpMidlet 将HttpMidlet.java文件拷贝至wtk目录下的apps/HttpMidlet的src目录下,将第三方kxml文件ksoap-midp.zip拷贝至apps/ HttpMidlet/lib目录下,编译即可

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读