来自:http://www.cnblogs.com/suleejia/archive/2011/05/22/2053167.html?login=1#commentform
OK~~接着前面写,这次介绍的是android调用.net的带soapheader的webservice,
布局文件很简单就一个button,网上有很多教程讲了android怎么调用webservice,好像带soapheader的很少~~好吧。。那就开始吧。。首先我们需要一个jar文件~~可以去网上搜ksoap2,我这里用的是ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar,具体代码见下面。。给出部分注释~~
01 | package Taling.ChangePwd; |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import org.kxml2.kdom.Element; |
06 | import org.kxml2.kdom.Node; |
08 | import android.view.View; |
09 | import android.widget.Button; |
10 | import android.widget.Toast; |
12 | import org.ksoap2.SoapEnvelope; |
13 | import org.ksoap2.serialization.PropertyInfo; |
14 | import org.ksoap2.serialization.SoapObject; |
15 | import org.ksoap2.serialization.SoapPrimitive; |
16 | import org.ksoap2.serialization.SoapSerializationEnvelope; |
17 | import org.ksoap2.transport.HttpTransportSE; |
19 | public class ChangePwdMain extends Activity { |
20 | private Button okButton; |
22 | public void onCreate(Bundle savedInstanceState) { |
23 | super.onCreate(savedInstanceState); |
24 | setContentView(R.layout.main); |
26 | okButton = (Button) this .findViewById(R.id.btnLogin); |
27 | okButton.setOnClickListener( new Button.OnClickListener() { |
29 | public void onClick(View v) { |
38 | private static final String METHOD_NAME = "UpdateUser" ; |
41 | private SoapPrimitive detail; |
42 | public void getInfo() { //主要代码在这里 |
44 | SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); |
45 | //此处2个propertyinfo,是updateuser方法所需的参数,代码下面贴出asmx代码 |
46 | PropertyInfo pi = new PropertyInfo(); |
50 | pi = new PropertyInfo(); |
51 | pi.setName( "password" ); |
55 | Element[] header = new Element[1]; |
56 | header[0] = new Element().createElement(NAMESPACE, "MySoapHeader" ); |
58 | Element username = new Element().createElement(NAMESPACE, "Admin" ); |
59 | username.addChild(Node.TEXT, "这里你填的" ); |
60 | header[0].addChild(Node.ELEMENT, username); |
61 | Element pass = new Element().createElement(NAMESPACE, "Password" ); |
62 | pass.addChild(Node.TEXT, "这里你填的" ); |
63 | header[0].addChild(Node.ELEMENT, pass); |
66 | SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); |
67 | envelope.headerOut = header; |
68 | envelope.bodyOut = rpc; |
69 | envelope.dotNet = true ; |
70 | envelope.setOutputSoapObject(rpc); |
71 | HttpTransportSE ht = new HttpTransportSE(URL); |
72 | ht.call(SOAP_ACTION, envelope); |
74 | detail =(SoapPrimitive) envelope.getResponse(); |
75 | Toast.makeText( this , detail.toString(), Toast.LENGTH_LONG).show(); |
77 | } catch (Exception e) { |
78 | Toast.makeText( this , e.getMessage(), Toast.LENGTH_SHORT).show(); |
1 | 上面代码header[0] = new Element().createElement(NAMESPACE, "MySoapHeader" ); 此处的MySoapHeader与上面asmx里的MySoapHeader要相同。。。OK。。。就到这吧。。希望对您有帮助~~ |
;