android客戶端傳遞複雜引數給webservice
1.Webservice開發
Webservice由wcf開發完成,我不懂,大概說一下會用到哪些。就在下圖中,解釋一下下圖,是伺服器自動生成的,通過URL(http://192.168.13.95:8086/UIServices)在瀏覽器中開啟是這樣的,程式中用到的就是URL和namespace,以及方法名,圖1的namespace1裡面定義的是方法名,namespace2裡面定義的是各種資料結構在圖4中(特別注意兩個名稱空間)。圖1和圖2是一個檔案,屬於namespace1,圖3和圖4是一個檔案屬於namespace2.
圖1
圖2
圖3
圖4
圖4中的資料結構在c#中是下面這樣定義的,生成
2.android呼叫
a) 首先要下載新增jar包:ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar,需要在網上下載現在應該到3.0的版本了。
b) 在android中,直接傳遞給ws引數時支援四種基本型別:int、String、boolean、long,傳遞基本型別時無須序列化,傳遞陣列是需要繼承vector(後面會講到),而且需要序列化(序列化具體我也不知道,有興趣的可以研究下)。
c) 呼叫ws的方法時需要傳遞相同型別的引數,基本型別就不多說了,還有一種是傳遞
d) 注意那幾個繼承下來的方法:裡面case的順序要和圖4中的各屬性順序一樣,case 0:必須是BlackColor,依次類推。getPropertyInfo中的arg2.namespace = 圖1中的namespace2,也就是圖3中的namespace這個很重要
e) 構造列表,android
f) 封裝成CaptionInfo類,每層都要序列化,此時引數(下面有程式碼)算是構造好了,然後是呼叫
g) 下面是呼叫的主要程式碼,以及類的寫法
<pre name="code" class="java">String nameSpaceString = "http://tempuri.org/";//圖1中的namespace1
String methodName = "Cantion";
String urlString = "http://192.168.13.95:8086/UIServices";//url
String soapAction = "http://tempuri.org/IUI/Cantion";//圖2中的soapAction
CaptionInfo captionInfo = new CaptionInfo();
SoapObject requests = new SoapObject(nameSpaceString, methodName);
requests.addProperty("message", captionInfo );//message 是伺服器中方法的引數 //名,最好保持一致
SoapSerializationEnvelope envelopes =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelopes.addMapping(CaptionInfo.NAMESPACE, "Caption", Caption.class);
envelopes.addMapping(CaptionInfo.NAMESPACE,
"ArrayOfCaption", ArrayOfCaption.class);
envelopes.addMapping(CaptionInfo.NAMESPACE, "CaptionInfo", CaptionInfo.class);
//envelopes.addMapping(...);必須加
envelopes.bodyOut = requests;
envelopes.dotNet = true;
envelopes.setOutputSoapObject(requests);
HttpTransportSE hts = new HttpTransportSE(urlString);
try {
hts.call(soapAction, envelopes);
if(envelopes.getResponse() != null)
{
//解析的時候沒找到好的解析方法,只能一步一步按照欄位解析
//可以單步除錯,看返回值是什麼
SoapObject returnObjects = (SoapObject) envelopes.bodyIn;
//SoapObject CaptionList =
(SoapObject)returnObjects.getProperty(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CaptionInfo的定義:
<pre name="code" class="java">
class Caption implements KvmSerializable{
public String BackColor;
public String CaptionName;
public String Content;
public String Font;
public String FontColor;
public String FontOpacity;
public String OutlineColor;
public String getCaptionName() {
return CaptionName;
}
public void setCaptionName(String captionName) {
CaptionName = captionName;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getFont() {
return Font;
}
public void setFont(String font) {
Font = font;
}
public String getFontColor() {
return FontColor;
}
public void setFontColor(String fontColor) {
FontColor = fontColor;
}
public String getBackColor() {
return BackColor;
}
public void setBackColor(String backColor) {
BackColor = backColor;
}
public String getFontOpacity() {
return FontOpacity;
}
public void setFontOpacity(String fontOpacity) {
FontOpacity = fontOpacity;
}
public String getOutlineColor() {
return OutlineColor;
}
public void setOutlineColor(String outlineColor) {
OutlineColor = outlineColor;
}
@Override
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
return BackColor;
case 1:
return CaptionName;
case 2:
return Content;
case 3:
return Font;
case 4:
return FontColor;
case 5:
return FontOpacity;
case 6:
return OutlineColor;
default:
return null;
}
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 7;//屬性的個數
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
// TODO Auto-generated method stub
arg2.namespace = "http://schemas.datacontract.org/2004/07/RecordUIObject";
switch (arg0) {
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "BackColor";
break;
case 1:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "CaptionName";
break;
case 2:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Content";
break;
case 3:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Font";
break;
case 4:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "FontColor";
break;
case 5:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "FontOpacity";
break;
case 6:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "OutlineColor";
break;
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
BackColor = arg1.toString();
break;
case 1:
CaptionName = arg1.toString();
break;
case 2:
Content = arg1.toString();
break;
case 3:
Font = arg1.toString();
break;
case 4:
FontColor = arg1.toString();
break;
case 5:
FontOpacity = arg1.toString();
break;
case 6:
OutlineColor = arg1.toString();
break;
default:
break;
}
}
}
class ArrayOfCaption extends Vector<Caption> implements KvmSerializable
{
@Override
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
return this.get(arg0);
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return this.size();
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
// TODO Auto-generated method stub
arg2.namespace = "http://schemas.datacontract.org/2004/07/RecordUIObject";
arg2.name = "Caption";
arg2.type = new Caption().getClass();
}
@Override
public void setProperty(int arg0, Object arg1) {
// TODO Auto-generated method stub
this.add((Caption)arg1);
}
}
public class CaptionInfo implements BaseObject{
public ArrayOfCaption CaptionList = new ArrayOfCaption();
public ArrayOfCaption getArrayOfCaption() {
return CaptionList;
}
public void setArrayOfCaption(ArrayOfCaption arrayOfCaption) {
this.CaptionList = arrayOfCaption;
}
@Override
public Object getProperty(int arg0) {
// TODO Auto-generated method stub
return CaptionList;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
// TODO Auto-generated method stub
arg2.namespace = "http://schemas.datacontract.org/2004/07/RecordUIObject";
arg2.type = PropertyInfo.VECTOR_CLASS;
arg2.name = "CaptionList";
}
@Override
public void setProperty(int arg0, Object arg1) {
// TODO Auto-generated method stub
CaptionList = (ArrayOfCaption)arg1;
}
}