Consumindo um Webservice via J2me

1 resposta
B

galera, boa noite!
consegui desenvolver um webservice em java e axis2 que se comunica com um bdzinho em mysql.Agora preciso de uma ajuda para fazer um programinha bem basico em j2me que consigo acessa-lo.O bd possui somente 5 rows como teste e nele existem 4 colunas sobre livros(id, autor, titulo e qtde). o webservice possui 2 operacoes que sao as que me interessam: titulodoautor(que listam os livros de um dado autor) e titulodotitulo(que listam os livros com um certo titulo) e sao acessadas atraves das linhas respectivas, por exemplo:

http://localhost:8080/axis2/services/DBSampleService/getTituloDoAutor?autor=dan%20brown
http://localhost:8080/axis2/services/DBSampleService/getTituloDoTitulo?titulo=anjos%20e%20demonios

vou postar a wsdl do meu web:

<?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://dbsample" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://dbsample/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://dbsample">
  <wsdl:documentation>DBSampleService</wsdl:documentation> 
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://dbsample/xsd">
- <xs:complexType name="Livro">
- <xs:sequence>
  <xs:element minOccurs="0" name="autor" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="id" type="xs:int" /> 
  <xs:element minOccurs="0" name="qtde" type="xs:int" /> 
  <xs:element minOccurs="0" name="titulo" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
- <xs:complexType name="TituloDoTitulo">
- <xs:sequence>
  <xs:element minOccurs="0" name="autor" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="id" type="xs:int" /> 
  <xs:element minOccurs="0" name="qtde" type="xs:int" /> 
  <xs:element minOccurs="0" name="titulo" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
- <xs:complexType name="TituloDoAutor">
- <xs:sequence>
  <xs:element minOccurs="0" name="autor" nillable="true" type="xs:string" /> 
  <xs:element minOccurs="0" name="id" type="xs:int" /> 
  <xs:element minOccurs="0" name="qtde" type="xs:int" /> 
  <xs:element minOccurs="0" name="titulo" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:schema>
- <xs:schema xmlns:ax22="http://dbsample/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://dbsample">
  <xs:import namespace="http://dbsample/xsd" /> 
- <xs:element name="listAllLivroResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element maxOccurs="unbounded" minOccurs="0" name="return" nillable="true" type="ax22:Livro" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getTituloDoTitulo">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="titulo" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getTituloDoTituloResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element maxOccurs="unbounded" minOccurs="0" name="return" nillable="true" type="ax22:TituloDoTitulo" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getTituloDoAutor">
- <xs:complexType>
- <xs:sequence>
  <xs:element minOccurs="0" name="autor" nillable="true" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
- <xs:element name="getTituloDoAutorResponse">
- <xs:complexType>
- <xs:sequence>
  <xs:element maxOccurs="unbounded" minOccurs="0" name="return" nillable="true" type="ax22:TituloDoAutor" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="getTituloDoAutorRequest">
  <wsdl:part name="parameters" element="ns:getTituloDoAutor" /> 
  </wsdl:message>
- <wsdl:message name="getTituloDoAutorResponse">
  <wsdl:part name="parameters" element="ns:getTituloDoAutorResponse" /> 
  </wsdl:message>
- <wsdl:message name="getTituloDoTituloRequest">
  <wsdl:part name="parameters" element="ns:getTituloDoTitulo" /> 
  </wsdl:message>
- <wsdl:message name="getTituloDoTituloResponse">
  <wsdl:part name="parameters" element="ns:getTituloDoTituloResponse" /> 
  </wsdl:message>
  <wsdl:message name="listAllLivroRequest" /> 
- <wsdl:message name="listAllLivroResponse">
  <wsdl:part name="parameters" element="ns:listAllLivroResponse" /> 
  </wsdl:message>
- <wsdl:portType name="DBSampleServicePortType">
- <wsdl:operation name="getTituloDoAutor">
  <wsdl:input message="ns:getTituloDoAutorRequest" wsaw:Action="urn:getTituloDoAutor" /> 
  <wsdl:output message="ns:getTituloDoAutorResponse" wsaw:Action="urn:getTituloDoAutorResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="getTituloDoTitulo">
  <wsdl:input message="ns:getTituloDoTituloRequest" wsaw:Action="urn:getTituloDoTitulo" /> 
  <wsdl:output message="ns:getTituloDoTituloResponse" wsaw:Action="urn:getTituloDoTituloResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="listAllLivro">
  <wsdl:input message="ns:listAllLivroRequest" wsaw:Action="urn:listAllLivro" /> 
  <wsdl:output message="ns:listAllLivroResponse" wsaw:Action="urn:listAllLivroResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="DBSampleServiceSoap11Binding" type="ns:DBSampleServicePortType">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getTituloDoAutor">
  <soap:operation soapAction="urn:getTituloDoAutor" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="getTituloDoTitulo">
  <soap:operation soapAction="urn:getTituloDoTitulo" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="listAllLivro">
  <soap:operation soapAction="urn:listAllLivro" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="DBSampleServiceSoap12Binding" type="ns:DBSampleServicePortType">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <wsdl:operation name="getTituloDoAutor">
  <soap12:operation soapAction="urn:getTituloDoAutor" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="getTituloDoTitulo">
  <soap12:operation soapAction="urn:getTituloDoTitulo" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="listAllLivro">
  <soap12:operation soapAction="urn:listAllLivro" style="document" /> 
- <wsdl:input>
  <soap12:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
  <soap12:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:binding name="DBSampleServiceHttpBinding" type="ns:DBSampleServicePortType">
  <http:binding verb="POST" /> 
- <wsdl:operation name="getTituloDoAutor">
  <http:operation location="DBSampleService/getTituloDoAutor" /> 
- <wsdl:input>
  <mime:content type="text/xml" part="getTituloDoAutor" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="text/xml" part="getTituloDoAutor" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="getTituloDoTitulo">
  <http:operation location="DBSampleService/getTituloDoTitulo" /> 
- <wsdl:input>
  <mime:content type="text/xml" part="getTituloDoTitulo" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="text/xml" part="getTituloDoTitulo" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="listAllLivro">
  <http:operation location="DBSampleService/listAllLivro" /> 
- <wsdl:input>
  <mime:content type="text/xml" part="listAllLivro" /> 
  </wsdl:input>
- <wsdl:output>
  <mime:content type="text/xml" part="listAllLivro" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="DBSampleService">
- <wsdl:port name="DBSampleServiceHttpSoap11Endpoint" binding="ns:DBSampleServiceSoap11Binding">
  <soap:address location="http://localhost:8080/axis2/services/DBSampleService.DBSampleServiceHttpSoap11Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="DBSampleServiceHttpSoap12Endpoint" binding="ns:DBSampleServiceSoap12Binding">
  <soap12:address location="http://localhost:8080/axis2/services/DBSampleService.DBSampleServiceHttpSoap12Endpoint/" /> 
  </wsdl:port>
- <wsdl:port name="DBSampleServiceHttpEndpoint" binding="ns:DBSampleServiceHttpBinding">
  <http:address location="http://localhost:8080/axis2/services/DBSampleService.DBSampleServiceHttpEndpoint/" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

alguem possui algo semi-pronto ou alguma dica de como conseguir acessar isso via j2me?

eu cheguei a dar uma olhada no exemplo abaixo de um programa que acessa um ws bem simples que retorna uma soma de dois valores passados por parametro, porem nao consegui altera-lo para usar com o meu ws acima:
# import javax.microedition.lcdui.Display;  
# import javax.microedition.lcdui.TextBox;  
#   
# import org.ksoap.SoapObject;  
# import org.ksoap.transport.HttpTransport;  
#   
# public class ClienteJ2ME extends javax.microedition.midlet.MIDlet {      
#   private Display display;          
#   private String url = "http://localhost:8080/axis/Servico.jws";   
#   TextBox textbox = null;   
#       
#   public void startApp() {   
#     display = Display.getDisplay(this);   
#     try {    
#       testWebService();   
#     } catch (Exception ex) {   
#       System.out.println(ex);      
#     }   
#   }   
#   
#   public void pauseApp() {}   
#   public void destroyApp(boolean unconditional) {}   
#   
#   public void testWebService() throws Exception {   
#     StringBuffer stringBuffer = new StringBuffer();   
#       
#     TextBox textBox = null;   
#       
#     // Chama o WebService  
#     SoapObject client = new  SoapObject(url,"soma");   
#     client.addProperty("valor1",new Integer(2));   
#     client.addProperty("valor2",new Integer(4));   
#     HttpTransport ht = new HttpTransport(url,"soma");  
#     stringBuffer.append("  
# Resultado: " + ht.call(client));  
#       
#     // mostra o valor do resultado na tela.  
#     textBox = new TextBox("Teste WebService", stringBuffer.toString(), 1024, 0);   
#     display.setCurrent(textBox);   
#   }   
# }

(fonte http://javafree.uol.com.br/artigo/871485/ )

se alguem conseguir ajudar, agradeço imensamente.

valeu!

1 Resposta

D

Olha, ninguem vai ler esse código todo… mas vc já mexeu com KSOAP ?

Criado 16 de dezembro de 2009
Ultima resposta 17 de dez. de 2009
Respostas 1
Participantes 2