幸运星空

Lucker的程序人生

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

来自: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;
02   
03 import android.app.Activity;
04 import android.os.Bundle;
05 import org.kxml2.kdom.Element;
06 import org.kxml2.kdom.Node;
07   
08 import android.view.View;
09 import android.widget.Button;
10 import android.widget.Toast;
11   
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;
18   
19 public class ChangePwdMain extends Activity {
20     private Button okButton;
21     @Override
22     public void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.main);
25           
26         okButton = (Button) this.findViewById(R.id.btnLogin);
27         okButton.setOnClickListener(new Button.OnClickListener() {
28             @Override
29             public void onClick(View v) {
30                   getInfo();  
31             }
32         });
33     }
34 //命名空间
35     private static final String NAMESPACE = "http://taling.hnie.edu.cn/";
36     private static String URL = "http://taling.hnie.edu.cn/sljchangepwd/changepwdservice.asmx";
37 //调用的方法名
38     private static final String METHOD_NAME = "UpdateUser";
39 //此处是命名空间+方法名
40     private static String SOAP_ACTION = "http://taling.hnie.edu.cn/UpdateUser";
41     private SoapPrimitive detail;
42     public void getInfo() {//主要代码在这里
43         try {
44             SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
45 //此处2个propertyinfo,是updateuser方法所需的参数,代码下面贴出asmx代码
46             PropertyInfo pi = new PropertyInfo(); 
47             pi.setName("uid");  
48             pi.setValue("24058");               
49             rpc.addProperty(pi);
50             pi = new PropertyInfo(); 
51             pi.setName("password");  
52             pi.setValue("321");               
53             rpc.addProperty(pi);
54             //soapheader在这里
55              Element[] header = new Element[1];
56                header[0] = new Element().createElement(NAMESPACE, "MySoapHeader");
57   
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);
64                  
65               
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);
73 //此处如果用soapobject会报错
74             detail =(SoapPrimitive) envelope.getResponse();
75             Toast.makeText(this, detail.toString(), Toast.LENGTH_LONG).show();
76             return;
77         } catch (Exception e) {
78             Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
79         }
80     }
81           
82     }
1 上面代码header[0] = new Element().createElement(NAMESPACE, "MySoapHeader"); 此处的MySoapHeader与上面asmx里的MySoapHeader要相同。。。OK。。。就到这吧。。希望对您有帮助~~
;
posted on 2011-08-09 10:09  Lucker  阅读(498)  评论(0编辑  收藏  举报