File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "calling .Net web Service by J2ME" Watch "calling .Net web Service by J2ME" New topic

calling .Net web Service by J2ME

Marco Battaglia

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>
public class Service1 : System.Web.Services.WebService
public Service1()
//CODEGEN: chiamata richiesta da Progettazione servizi Web ASP.NET.
#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)

// 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.
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 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,
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);
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
"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="" xmlns:xsd="" xmlns:soap="">
<sayHello xmlns="urn:Hello" />
Michael Yuan
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:
I agree. Here's the link:
subject: calling .Net web Service by J2ME
It's not a secret anymore!