Hi,
I am new to J2ME, just started
working on a project where my J2ME application needs
to receive/send data to .NET web service. I have written a sample
code to test the service. I was able to get the data from the server.
Now, my question here
is:
1. How do I parse and get the data
from the response I got from the server since it contains lot of
things?
2. I have this
XML
<Cust_Appointment><Record><Internal_Cust_Id>1</Internal_Cust_Id><Appointment_Type>1</Appointment_Type><Appointment_Date>5-June-2003</Appointment_Date><User_Id>1</User_Id><Description><![CDATA[Mobile
Demo]]></Description><Active_Date>5-June-2002</Active_Date><Inactive_Date>
5-June-2004</Inactive_Date></Record></Cust_Appointment>
How do I send it to the Server?? I
was able to connect to the server but it returned a Fault as
below:
Buffer:<?xml version="1.0"
encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Server was unable to read request. --> The
&lt;Cust_Appointment&gt; tag from namespace is expected.
Line 1, position 2.</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
Here is the sample I am using
right now for reading/sending data from/to the
server:
package
soaptest;
import java.io.*;
import
javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import
javax.microedition.io.*;
import org.ksoap.*;
import
org.kxml.*;
import org.kxml.io.*;
import
org.kxml.parser.*;
public class clsHTTP extends Form
implements CommandListener {
// static final String
serviceNamespace = "http://pocketdba.com/webservices/";
static final String serviceNamespace = "http://tempuri.org/";
static final
String
xmlData="<Cust_Appointment><Record><Internal_Cust_Id>1</Internal_Cust_Id><Appointment_Type>1</Appointment_Type><Appointment_Date>5-June-2003</Appointment_Date><User_Id>1</User_Id><Description><![CDATA[Mobile
Demo]]></Description><Active_Date>5-June-2002</Active_Date><Inactive_Date>5-June-2004</Inactive_Date></Record></Cust_Appointment>";
TextField fromField = new TextField ("UserName :", "ss", 4,
TextField.ANY);
StringItem resultItem = new StringItem ("",
"");
Command getCommand = new Command ("Get", Command.SCREEN,
1);
Command sendCommand = new Command ("Send", Command.SCREEN,
2);
public clsHTTP()
{
super("Get Data");
append
(fromField);
append (resultItem);
addCommand (getCommand);
addCommand
(sendCommand);
setCommandListener (this);
}
public void commandAction
(Command c, Displayable d) {
if(c.getLabel()=="Get"){
try
{
String from =
fromField.getString ();
ByteArrayOutputStream bos = new ByteArrayOutputStream
();
SoapObject request = new
SoapObject (serviceNamespace,
"DownloadAppointmentConfig");
request.addProperty ("uName", fromField.getString ());
ClassMap classMap = new
ClassMap();
classMap.prefixMap =
new PrefixMap(classMap.prefixMap, "tns", "http://tempuri.org/");
SoapEnvelope envelope = new SoapEnvelope
(classMap);
envelope.setBody
(request);
XmlWriter xw = new XmlWriter (new OutputStreamWriter
(bos));
envelope.write
(xw);
xw.flush
();
byte [] data =
""> //
System.out.println (new String (data));
HttpConnection connection = (HttpConnection)
Connector.open
("http://MMSServer/SyncSvr/SyncSvrObj.asmx",Connector.READ_WRITE);
connection.setRequestMethod
(HttpConnection.POST);
connection.setRequestProperty ("Content-Type",
"text/xml");
connection.setRequestProperty ("Connection",
"close");
connection.setRequestProperty ("Content-Length",
""+data.length);
connection.setRequestProperty("SOAPAction","\"http://tempuri.org/DownloadAppointmentConfig\"");
OutputStream os = connection.openOutputStream
();
os.write
(data);
os.flush
();
InputStream is=((StreamConnection) connection).openInputStream
();
int
buffSize=200;
byte[] buff = new
byte[buffSize];
ByteArrayOutputStream bytebuff=new
ByteArrayOutputStream(buffSize);
int eof=0;
while(eof!=-1)
{
eof=is.read(buff);
if (eof!=-1)
{
bytebuff.write(buff,0,eof);
}
}
is.close();
String response = new
String(bytebuff.toByteArray());
System.out.println (response);
/*
Reader reader = new
InputStreamReader(new
ByteArrayInputStream(bytebuff.toByteArray()));
XmlParser parser = new XmlParser
(reader);
SoapEnvelope
resultEnvelope = new SoapEnvelope
();
resultEnvelope.parse
(parser);
resultItem.setLabel
("amount :");
String result =
(String)resultEnvelope.getResult();
result =
result.substring(0,result.indexOf(".")+3);
resultItem.setText (" "+result);
reader.close ();
*/
connection.close
();
os.close
();
}
catch (Exception e)
{
resultItem.setLabel
("Error:");
resultItem.setText
(e.toString ());
}
}
else{
/*
Sending data to SERVER*/
try
{
String from =
fromField.getString ();
ByteArrayOutputStream bos = new ByteArrayOutputStream
();
SoapObject request = new
SoapObject (serviceNamespace,
"Upload_Cust_Appointment");
request.addProperty ("uName", fromField.getString
());
request.addProperty
("fileName", "Cust_Appointment");
ClassMap classMap = new
ClassMap();
classMap.prefixMap =
new PrefixMap(classMap.prefixMap, "tns", "http://tempuri.org/");
SoapEnvelope envelope = new SoapEnvelope
(classMap);
envelope.setBody
(request);
XmlWriter xw = new XmlWriter (new OutputStreamWriter
(bos));
envelope.write
(xw);
xw.flush
();
byte [] data = "">
HttpConnection connection = (HttpConnection)
Connector.open
("http://MMSServer/SyncSvr/SyncSvrObj.asmx",Connector.READ_WRITE);
connection.setRequestMethod
(HttpConnection.POST);
connection.setRequestProperty ("Content-Type",
"text/xml");
connection.setRequestProperty ("Connection",
"close");
connection.setRequestProperty ("Content-Length", "" +
data.length);
connection.setRequestProperty("SOAPAction","\"http://tempuri.org/Upload_Cust_Appointment\"");
OutputStream os = connection.openOutputStream
();
os.write
(data);
// os.write
(xmlData.getBytes());
os.flush
();
os.close
();
InputStream is=((StreamConnection) connection).openInputStream
();
int
buffSize=200;
byte[] buff = new
byte[buffSize];
ByteArrayOutputStream bytebuff = new
ByteArrayOutputStream(buffSize);
int eof=0;
while(eof!=-1)
{
eof=is.read(buff);
if (eof!=-1)
{
bytebuff.write(buff,0,eof);
}
}
is.close();
String response = new
String(bytebuff.toByteArray());
System.out.println (response);
connection = (HttpConnection)
Connector.open
("http://MMSServer/SyncSvr/SyncSvrObj.asmx",Connector.READ_WRITE);
connection.setRequestMethod
(HttpConnection.POST);
connection.setRequestProperty ("Content-Type",
"text/xml");
connection.setRequestProperty ("Connection",
"close");
connection.setRequestProperty ("Content-Length", "" +
xmlData.length());
connection.setRequestProperty("SOAPAction","\"http://tempuri.org/Upload_Cust_Appointment\"");
os = connection.openOutputStream ();
os.write (xmlData.getBytes());
os.flush ();
int datum;
long
byteCount;
StringBuffer buffer =
new StringBuffer();
is=((StreamConnection) connection).openInputStream ();
for(byteCount = 0; (datum=is.read()) > 0; byteCount++)
{
buffer.append((char)datum);
}
os.close ();
System.out.println("==============buff=====================");
System.out.println("Buffer:" +
buffer.toString());
System.out.println("===================================");
}
catch (Exception e)
{
resultItem.setLabel
("Error:");
resultItem.setText
(e.toString ());
}
}
}
private static byte[]
extractXml(byte[] extractFrom){
byte[]
result;
int off=0;
boolean
nonStop=true;
for(int
i=0;nonStop;i++){
if (extractFrom[i]==60)
{
off=i;
nonStop=false;
}
}
result=new
byte[extractFrom.length -off];
for(int
i=off;i<extractFrom.length;i++){
result[i-off]=extractFrom[i];
}
return result;
}
}
Any help would be
appreciated.
Thanks in
advance
Bhanu