Web Service makes me crazy..[SOLVED]

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Web Service makes me crazy..[SOLVED]

Postby Malefico » Tue Nov 25, 2008 6:10 pm

Im tring to use a web service implemented in .net

AND THIS VERSION WORKS!

this is my very simple Web service ( HelloWorld ):

ps:It's C# not java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. namespace WebService1
  3.  
  4. {
  5.  
  6.  
  7.  
  8.     [WebService(Namespace = "http://tempuri.org/")]
  9.  
  10.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11.  
  12.     [ToolboxItem(false)]  
  13.  
  14.  
  15.  
  16.     public class Service1 : System.Web.Services.WebService
  17.  
  18.     {
  19.  
  20.  
  21.  
  22.         [WebMethod]
  23.  
  24.         public string HelloWorld()
  25.  
  26.         {
  27.  
  28.             return "Hello World";
  29.  
  30.         }
  31.  
  32.     }
  33.  
  34. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


And its WSDL
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  4.  
  5.   <wsdl:types>
  6.  
  7.     <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
  8.  
  9.       <s:element name="HelloWorld">
  10.  
  11.         <s:complexType />
  12.  
  13.       </s:element>
  14.  
  15.       <s:element name="HelloWorldResponse">
  16.  
  17.         <s:complexType>
  18.  
  19.           <s:sequence>
  20.  
  21.  
  22.  
  23.             <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
  24.  
  25.           </s:sequence>
  26.  
  27.         </s:complexType>
  28.  
  29.       </s:element>
  30.  
  31.     </s:schema>
  32.  
  33.   </wsdl:types>
  34.  
  35.   <wsdl:message name="HelloWorldSoapIn">
  36.  
  37.     <wsdl:part name="parameters" element="tns:HelloWorld" />
  38.  
  39.   </wsdl:message>
  40.  
  41.  
  42.  
  43.   <wsdl:message name="HelloWorldSoapOut">
  44.  
  45.     <wsdl:part name="parameters" element="tns:HelloWorldResponse" />
  46.  
  47.   </wsdl:message>
  48.  
  49.   <wsdl:portType name="Service1Soap">
  50.  
  51.     <wsdl:operation name="HelloWorld">
  52.  
  53.       <wsdl:input message="tns:HelloWorldSoapIn" />
  54.  
  55.       <wsdl:output message="tns:HelloWorldSoapOut" />
  56.  
  57.     </wsdl:operation>
  58.  
  59.   </wsdl:portType>
  60.  
  61.  
  62.  
  63.   <wsdl:binding name="Service1Soap" type="tns:Service1Soap">
  64.  
  65.     <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
  66.  
  67.     <wsdl:operation name="HelloWorld">
  68.  
  69.       <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
  70.  
  71.       <wsdl:input>
  72.  
  73.         <soap:body use="literal" />
  74.  
  75.       </wsdl:input>
  76.  
  77.       <wsdl:output>
  78.  
  79.         <soap:body use="literal" />
  80.  
  81.  
  82.  
  83.       </wsdl:output>
  84.  
  85.     </wsdl:operation>
  86.  
  87.   </wsdl:binding>
  88.  
  89.   <wsdl:binding name="Service1Soap12" type="tns:Service1Soap">
  90.  
  91.     <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
  92.  
  93.     <wsdl:operation name="HelloWorld">
  94.  
  95.       <soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
  96.  
  97.       <wsdl:input>
  98.  
  99.         <soap12:body use="literal" />
  100.  
  101.  
  102.  
  103.       </wsdl:input>
  104.  
  105.       <wsdl:output>
  106.  
  107.         <soap12:body use="literal" />
  108.  
  109.       </wsdl:output>
  110.  
  111.     </wsdl:operation>
  112.  
  113.   </wsdl:binding>
  114.  
  115.   <wsdl:service name="Service1">
  116.  
  117.     <wsdl:port name="Service1Soap" binding="tns:Service1Soap">
  118.  
  119.       <soap:address location="http://localhost/WebService1/Service1.asmx" />
  120.  
  121.  
  122.  
  123.     </wsdl:port>
  124.  
  125.     <wsdl:port name="Service1Soap12" binding="tns:Service1Soap12">
  126.  
  127.       <soap12:address location="http://localhost/WebService1/Service1.asmx" />
  128.  
  129.     </wsdl:port>
  130.  
  131.   </wsdl:service>
  132.  
  133. </wsdl:definitions>
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


Im tring to call it with this android code (192.168.1.81 is my ip) :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class WSAndroid extends Activity {
  3.  
  4.         private Button okButton;
  5.  
  6.         private TextView result;
  7.  
  8.         private Object resultRequestSoap = null;
  9.  
  10.         private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
  11.  
  12.         private static final String METHOD_NAME = "HelloWorld";
  13.  
  14.         private static final String NAMESPACE = "http://tempuri.org/";
  15.  
  16.         private static final String URL = "http://192.168.1.81/Service1/Service1.asmx?wsdl";
  17.  
  18.  
  19.  
  20.         @Override
  21.  
  22.         protected void onCreate(Bundle icicle) {
  23.  
  24.                 super.onCreate(icicle);      
  25.  
  26.                 setContentView(R.layout.main);
  27.  
  28.                 result = (TextView) findViewById(R.id.result);
  29.  
  30.                 okButton = (Button)      findViewById(R.id.invio);
  31.  
  32.  
  33.  
  34.                 okButton.setOnClickListener(new Button.OnClickListener() {
  35.  
  36.                         public void onClick(View v) {            
  37.  
  38.                                 try {
  39.  
  40.                                         callWebService();
  41.  
  42.                                 } catch (IOException e) {
  43.  
  44.                                         // TODO Auto-generated catch block
  45.  
  46.                                         e.printStackTrace();
  47.  
  48.                                 } catch (XmlPullParserException e) {
  49.  
  50.                                         // TODO Auto-generated catch block
  51.  
  52.                                         e.printStackTrace();
  53.  
  54.                                 }
  55.  
  56.                         }
  57.  
  58.                 });
  59.  
  60.         }
  61.  
  62.         public void callWebService() throws IOException, XmlPullParserException{
  63.  
  64.                 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  65.  
  66.                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  67.  
  68.                 envelope.dotNet = true;    
  69.  
  70.                 envelope.setOutputSoapObject(request);      
  71.  
  72.                 HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  73.  
  74.                 try {
  75.  
  76.                         androidHttpTransport.call(SOAP_ACTION, envelope);
  77.  
  78.                         resultRequestSoap = envelope.getResponse();
  79.  
  80.                         //Parse Response
  81.  
  82.                         SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
  83.  
  84.                         //Return a String for view
  85.  
  86.                         result.setText(resultsRequestSOAP.getProperty(0).toString());
  87.  
  88.                 } catch (Exception e) {
  89.  
  90.                         e.printStackTrace();
  91.  
  92.                 }
  93.  
  94.  
  95.  
  96.         }
  97.  
  98. }
  99.  
  100.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4



You can use this as simple base tutorial to access a web service using ksoap2
Malefico
Developer
Developer
 
Posts: 30
Joined: Fri Oct 24, 2008 3:32 pm

Top

Postby borismir » Tue Jul 14, 2009 9:32 am

First of all, thank you for this simple tutorial. That was exactly what i needed, because I'm quite unexperienced with Android and Java.

But now I have a question on how to take it a step further. How can I read custom classes? For example if i had this webmethod:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. namespace WebService1
  3.  
  4. {
  5.  
  6.  
  7.  
  8.     [WebService(Namespace = "http://tempuri.org/")]
  9.  
  10.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11.  
  12.     [ToolboxItem(false)]  
  13.  
  14.  
  15.  
  16.     public class Service1 : System.Web.Services.WebService
  17.  
  18.     {
  19.  
  20.  
  21.  
  22.         [WebMethod]
  23.  
  24.         public HelloWorldResult HelloWorld()
  25.  
  26.         {
  27.  
  28.             return new HelloWorldResult();
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         public class HelloWorldResult
  35.  
  36.         {
  37.  
  38.            string a = "Hello";
  39.  
  40.            string b = "World";
  41.  
  42.         }
  43.  
  44.     }
  45.  
  46. }
  47.  
  48.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


As Result i get something like {HelloWorldResult=anyType{};}.

And something else to your code: when i run it, I only get a result every second time I press the button. The other times I get this error: "unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@43618678)"
Any Idea why? Or do you know this behaviour?

Thanks a lot in advance!
borismir
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jul 14, 2009 8:31 am

Postby desuvinodkumar » Wed Jul 15, 2009 6:53 am

HI

Its good

how u are going to parse ur response from soap.....

i also got repsonse like this
getRatingResponse{return=ContestInfo{item=anyType{name=Ankitha; totalimages=2; rating=2.5; };

what is this format u know ......

please help me regarding this me also ....

thanks a,lot in advance
Vinod...
User avatar
desuvinodkumar
Senior Developer
Senior Developer
 
Posts: 102
Joined: Mon Mar 23, 2009 6:51 am
Location: India

Postby ohyes » Fri Aug 14, 2009 9:28 am

Hi,
How can I fix this bug (androidHttpTransport.call(SOAP_ACTION, envelope); causes an exception)

My Java code is the following:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
  2.  
  3.    private static final String METHOD_NAME = "HelloWorld";
  4.  
  5.    private static final String NAMESPACE = "http://tempuri.org/";
  6.  
  7.    private static final String URL = "http://127.0.0.1:1483/WebSite2/Service.asmx";
  8.  
  9.  
  10.  
  11.    @Override
  12.  
  13.    public void onCreate(Bundle savedInstanceState) {
  14.  
  15.       super.onCreate(savedInstanceState);
  16.  
  17.       setContentView(R.layout.main);
  18.  
  19.  
  20.  
  21.       mResult = (EditText) findViewById(R.id.result);
  22.  
  23.       try {
  24.  
  25.          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  26.  
  27.  
  28.  
  29.          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  30.  
  31.          envelope.dotNet = true;
  32.  
  33.          envelope.setOutputSoapObject(request);
  34.  
  35.          AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
  36.  
  37.          try {
  38.  
  39.             <span style="font-weight: bold">androidHttpTransport.call(SOAP_ACTION, envelope);</span> //Throw exception here!!!
  40.  
  41.             SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
  42.  
  43.             mResult.setText("Ganzes Rückagbeobjekt:\n");// +resultsRequestSOAP.toString());
  44.  
  45.          }
  46.  
  47.  
  48.  
  49.          catch (Exception e) {
  50.  
  51.             mResult.setText("NG1\n");
  52.  
  53.          }
  54.  
  55.  
  56.  
  57.       } catch (Exception e) {
  58.  
  59.          mResult.setText("NG2\n");
  60.  
  61.       } finally {
  62.  
  63.          mResult.setText("NG3\n");
  64.  
  65.       }
  66.  
  67.    }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4



The C# code is as follow:
Code: Select all

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }   
}


Thank you in advance,

---
Ohyes
ohyes
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Jul 10, 2009 2:51 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest