It's not a secret anymore!*
The moose likes Java Micro Edition and the fly likes calling .Net web Service by J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "calling .Net web Service by J2ME" Watch "calling .Net web Service by J2ME" New topic
Author

calling .Net web Service by J2ME

Marco Battaglia
Greenhorn

Joined: Feb 17, 2004
Posts: 4
I've problem configuring the correct HttpTransport to call my .net Web Service. Could anyone help me ?? Please?
I think there is a mistake in in my new HttpProtocol("","") constructor invocation.
Thanx, Thanx in advance!!

this is my web service:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace WS
{
/// <summary>
/// Descrizione di riepilogo per Service1.
/// </summary>
[WebService(Namespace="urn:Hello")]
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: chiamata richiesta da Progettazione servizi Web ASP.NET.
InitializeComponent();
}
#region Component Designer generated code

//Richiesto da Progettazione servizi Web
private IContainer components = null;

/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion
// ESEMPIO DI SERVIZIO WEB
// Il servizio di esempio HelloWorld() restituisce la stringa Hello World.
// Per generare, rimuovere i commenti dalle righe seguenti, quindi salvare e generare il progetto.
// Per verificare il servizio Web, premere F5.
[WebMethod]
public string sayHello()
{
return "Hello World";
}
}
}
and this is my simplest J2me code:

import java.lang.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
import org.ksoap.*;
import org.ksoap.transport.*;
public class mio extends MIDlet implements
CommandListener, Runnable {
Form mainForm = new Form ("StockQuotes");
TextField symbolField = new TextField ("Symbol", "", 5,
TextField.ANY);
StringItem resultItem = new StringItem ("", "");
Command getCommand = new Command ("Get", Command.SCREEN, 1);
public mio () {
mainForm.append (symbolField);
mainForm.append (resultItem);
mainForm.addCommand (getCommand);
mainForm.setCommandListener (this);
}
public void startApp () {
Display.getDisplay (this).setCurrent (mainForm);
}
public void pauseApp () {
}
public void destroyApp (boolean unconditional) {
}
public void commandAction (Command c, Displayable d) {

Thread th= new Thread(this);
th.start();
}
public void run(){

try {
// Build request string
String symbol = symbolField.getString ();
resultItem.setLabel (symbol);
// Create a SoapObject by specifying the URN and themethod name
// of the SOAP RPC Web Service.
SoapObject rpc = new SoapObject
("http://localhost/WS/Service1.asmx", "sayHello");
// The addProperty method allows you to specify parametersto for
// the method used.
// rpc.addProperty ("symbol", symbol);
// The HttpTransport class can be used to make the actualcall.
// Its constructor accepts the Web Service endpoint as well as
// the method to be called.

resultItem.setText (""+new HttpTransport
("http://localhost/WS/Service1.asmx",
"urn:Hello#sayHello").call (rpc));
}
catch (Exception e) {
e.printStackTrace ();
resultItem.setLabel ("Error:");
resultItem.setText (e.toString ());


}
}

public static void main (String [] argv) {
new mio ().startApp ();
}
}
and this is the soap request that my web service ask:
POST /WS/Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "urn:Hello/sayHello"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<sayHello xmlns="urn:Hello" />
</soap:Body>
</soap:Envelope>
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
I recall kSOAP v1.0 requires the SOAP server to explicitly encode the type. For example, you need:

But .NET servers often givens literal SOAP messages with "default" types like this one:

I believe kSOAP v2 has a "dotnet" tag in its deserialize class and it is supposed to fix that problem.


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calling .Net web Service by J2ME