OK~~接着前面写,这次介绍的是android调用.net的带soapheader的webservice,
布局文件很简单就一个button,网上有很多教程讲了android怎么调用webservice,好像带soapheader的很少~~好吧。。那就开始吧。。首先我们需要一个jar文件~~可以去网上搜ksoap2,我这里用的是ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar,具体代码见下面。。给出部分注释~~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
package Taling.ChangePwd;
import android.app.Activity;
import android.os.Bundle;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public
class
ChangePwdMain extends Activity {
private
Button okButton;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button)
this
.findViewById(R.id.btnLogin);
okButton.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View v) {
getInfo();
}
});
}
//命名空间
//调用的方法名
private
static
final String METHOD_NAME =
"UpdateUser"
;
//此处是命名空间+方法名
private
SoapPrimitive detail;
public
void
getInfo() {
//主要代码在这里
try
{
SoapObject rpc =
new
SoapObject(NAMESPACE, METHOD_NAME);
//此处2个propertyinfo,是updateuser方法所需的参数,代码下面贴出asmx代码
PropertyInfo pi =
new
PropertyInfo();
pi.setName(
"uid"
);
pi.setValue(
"24058"
);
rpc.addProperty(pi);
pi =
new
PropertyInfo();
pi.setName(
"password"
);
pi.setValue(
"321"
);
rpc.addProperty(pi);
//soapheader在这里
Element[] header =
new
Element[1];
header[0] =
new
Element().createElement(NAMESPACE,
"MySoapHeader"
);
Element username =
new
Element().createElement(NAMESPACE,
"Admin"
);
username.addChild(Node.TEXT,
"这里你填的"
);
header[0].addChild(Node.ELEMENT, username);
Element pass =
new
Element().createElement(NAMESPACE,
"Password"
);
pass.addChild(Node.TEXT,
"这里你填的"
);
header[0].addChild(Node.ELEMENT, pass);
SoapSerializationEnvelope envelope =
new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.headerOut = header;
envelope.bodyOut = rpc;
envelope.dotNet =
true
;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht =
new
HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
//此处如果用soapobject会报错
detail =(SoapPrimitive) envelope.getResponse();
Toast.makeText(
this
, detail.toString(), Toast.LENGTH_LONG).show();
return
;
}
catch
(Exception e) {
Toast.makeText(
this
, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
|
1
|
|
1
|
上面代码header[0] =
new
Element().createElement(NAMESPACE,
"MySoapHeader"
); 此处的MySoapHeader与上面asmx里的MySoapHeader要相同。。。OK。。。就到这吧。。希望对您有帮助~~
|
![](http://pic002.cnblogs.com/images/2011/206198/2011052200162515.jpg)