Two methods to call WebService in Android

Source: Internet
Author: User

First: Use the ksoap2 Library

Original post example from

Http://www.cnblogs.com/ghj1976/archive/2011/04/26/2028904.html

It looks good, but I ended up failing.

The first crash was due to version issues. I do not know how to download the latest 2.5.7 on the home page. The compilation is okay, and the program crashes once it is run. After that, I finally succeeded in switching to 2.4.

I don't know why for the second time. The example can run smoothly, but my own program does not survive and crashes again.

 

The original post analysis is good. copy it.

1. Specify the WebService namespace and call Method

import org.ksoap2.serialization.SoapObject;private static final String NAMESPACE = "http://WebXml.com.cn/";private static final String METHOD_NAME = "getWeatherbyCityName";SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);

The first parameter of the soapobject class represents the WebService namespace. You can find the WebService namespace from the WSDL document.
The second parameter indicates the name of the WebService method to be called.

2. Set the parameter value of the method to be called. If there is no parameter, it can be omitted. The code for setting the parameter value of the method is as follows:

Rpc. addproperty ("thecityname", "Beijing ");

Note that although the 1st parameters of the addproperty method represent the parameter name of the method to be called, the parameter value is not necessarily the same as the method parameter name in the WebService class of the server, you only need to set parameters in the same order.

3. Generate the SOAP request information that calls the WebService method.

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = rpc;envelope.dotNet = true;envelope.setOutputSoapObject(rpc);

When creating a soapserializationenvelope object, you must use the soapserializationenvelope class constructor to set the SOAP Protocol version number.
This version number must be set according to the WebService version number on the server.
After creating a soapserializationenvelope object, do not forget to set the bodyout attribute of the soapsoapserializationenvelope class,
The value of this attribute is the soapobject created in step 1.

4. Create an httptransportsse object.

Do not use androidhttptransport ht = new androidhttptransport (URL) here; this is a class to expire

private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";HttpTransportSE ht = new HttpTransportSE(URL); 
ht.debug = true;

5. Use the call method to call the WebService Method

private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";ht.call(SOAP_ACTION, envelope);

Someone on the Internet said that the first parameter of the call here is null, but after my test, null is not acceptable.
The 2nd parameters are the soapserializationenvelope object created in step 1.

6. Obtain the returned results of the WebService method.

There are two methods:

1. Use the getresponse method to obtain the returned data.

private SoapObject detail;detail =(SoapObject) envelope.getResponse();

2. Use bodyin and getproperty.

private SoapObject detail;SoapObject result = (SoapObject)envelope.bodyIn;detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");

7. An error occurs at this time, prompting you that you do not have the permission to access the network.

You need to modify the androidmanifest. xml file and grant the corresponding permissions.

Simply add the following configuration line: <uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission>

 

Method 2: directly write and read XML strings

Original post from

Http://hi.baidu.com/lbp0408/blog/item/7971ae10d229b30c203f2e12.html/cmtid/d50a2ff4ca6692e07709d7e7

It is quite helpful, that is, sending a qualified XML string to WebService and then reading the returned XML string.

However, it takes some time to parse XML.

Final string server_url = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; // define the source URL of the content to be obtained url = new URL (server_url); urlconnection con = URL. openconnection (); con. setdooutput (true); con. setrequestproperty ("Pragma:", "No-Cache"); con. setrequestproperty ("cache-control", "No-Cache"); con. setrequestproperty ("Content-Type", "text/XML"); outputstreamwriter out = new outputstreamwriter (con. ge Toutputstream (); string xmlinfo = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ N "+" <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/\"> \ n "+" <soap: body> \ n "+" <getweatherbycityname xmlns = \ "http://WebXml.com.cn/\"> \ n "+" <thecityname> Beijing </thecityname> \ n "+" </getweatherbycityname> \ n "+" </soap: body> \ n "+" </soap: envelope> \ n "; toast. maketext (this, xmlinfo, Toast. length_long ). show (); send out. write (new string (xmlinfo. getbytes ("UTF-8"); out. flush (); out. close (); // obtain the returned value bufferedreader BR = new bufferedreader (New inputstreamreader (con. getinputstream (); stringbuilder sbuilder = new stringbuilder (); string line = ""; for (line = BR. readline (); line! = NULL; line = Br. Readline () {sbuilder. append (line );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.