J2ME Access Dotnetwerbservice

Source: Internet
Author: User
Tags exit copy gettext connect thread tostring xml parser
Visit J2ME access dotnetwerbservice[share]

Post by:chinapeople @ 2003-9-21 12:51:55

1. Thinking: Using the J2ME itself with the Httpconnection access WebService, call the Http://localhost/RoadWebService/RoadWS.asmx/method WebServiceTest, parameter is param. As follows:

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;

Here is the right to the string s for XML parsing.

Items = Parseusingkxml (s);

Mdisplay.setcurrent (new ItemList (items));

}



This time the stream to the string is in XML format, as follows: Hello ZL

Using a third party's CLDC environment, the open source XML parser ──kxml, download the Kxml package from the http://www.kxml.org/. The data stream in the obtained XML format is parsed by the following methods:

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 ();

}

}

}

After parsing the function parsekxmlitems the data is exported to the items and used for display.

Appendix 1:

The method of WebService in Dotnet WebServiceTest (specifically to establish a dotnetwebservie please refer to other information):

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

Appendix 2

Client J2ME Source code://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 ("Exit", Command.exit, 0);

Mconnectcommand = new Command ("Connection",

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 ();

}

}

}

}

Use wtk2.0 to build project with the name Httpmidlet, class name Httpmidlet

Copy the Httpmidlet.java file to the Apps\httpmidlet src directory under the WTK directory, and copy the third party Kxml file ksoap-midp.zip to the Apps\ directory and compile it.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.