【移動應用開發(fā)技術】Android中怎么與服務器端數(shù)據(jù)進行交互_第1頁
【移動應用開發(fā)技術】Android中怎么與服務器端數(shù)據(jù)進行交互_第2頁
【移動應用開發(fā)技術】Android中怎么與服務器端數(shù)據(jù)進行交互_第3頁
【移動應用開發(fā)技術】Android中怎么與服務器端數(shù)據(jù)進行交互_第4頁
【移動應用開發(fā)技術】Android中怎么與服務器端數(shù)據(jù)進行交互_第5頁
免費預覽已結束,剩余2頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】Android中怎么與服務器端數(shù)據(jù)進行交互

ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar/upload/information/20210522/379/541014.jpg//命名空間

private

static

final

String

serviceNameSpace="http://WebX/";

//調用方法(獲得支持的城市)

private

static

final

String

getSupportCity="getSupportCity";

//實例化SoapObject對象

SoapObject

request=new

SoapObject(serviceNameSpace,

getSupportCity);request.addProperty("參數(shù)名稱","參數(shù)值");//獲得序列化的Envelope

SoapSerializationEnvelope

envelope=new

SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.bodyOut=request;?(new

MarshalBase64()).register(envelope);//請求URL

private

static

final

String

serviceURL="/webservices/weatherwebservice.asmx";

//Android傳輸對象

AndroidHttpTransport

transport=new

AndroidHttpTransport(serviceURL);

transport.debug=true;transport.call(serviceNameSpace+getWeatherbyCityName,

envelope);if(envelope.getResponse()!=null){

return

parse(envelope.bodyIn.toString());

}

/**************

*

解析XML

*

@param

str

*

@return

*/

private

static

List<String>

parse(String

str){

String

temp;

List<String>

list=new

ArrayList<String>();

if(str!=null

&&

str.length()>0){

int

start=str.indexOf("string");

int

end=str.lastIndexOf(";");

temp=str.substring(start,

end-3);

String

[]test=temp.split(";");

for(int

i=0;i<test.length;i++){

if(i==0){

temp=test[i].substring(7);

}else{

temp=test[i].substring(8);

}

int

index=temp.indexOf(",");

list.add(temp.substring(0,

index));

}

}

return

list;

}//命名空間

private

static

final

String

serviceNameSpace="http://WebX/";

//請求URL

private

static

final

String

serviceURL="/webservices/weatherwebservice.asmx";

//調用方法(獲得支持的城市)

private

static

final

String

getSupportCity="getSupportCity";

//調用城市的方法(需要帶參數(shù))

private

static

final

String

getWeatherbyCityName="getWeatherbyCityName";

//調用省或者直轄市的方法(獲得支持的省份或直轄市)

private

static

final

String

getSupportProvince="getSupportProvince";<?xml

version="1.0"

encoding="utf-8"

?>

<ArrayOfString

xmlns:xsi="/2001/XMLSchema-instance"

xmlns:xsd="/2001/XMLSchema"

xmlns="http://WebX/">

<string>直轄市</string>

<string>特別行政區(qū)</string>

<string>黑龍江</string>

<string>吉林</string>

<string>遼寧</string>

<string>內蒙古</string>

<string>河北</string>

<string>河南</string>

<string>山東</string>

<string>山西</string>

<string>江蘇</string>

<string>安徽</string>

<string>陜西</string>

<string>寧夏</string>

<string>甘肅</string>

<string>青海</string>

<string>湖北</string>

<string>湖南</string>

<string>浙江</string>

<string>江西</string>

<string>福建</string>

<string>貴州</string>

<string>四川</string>

<string>廣東</string>

<string>廣西</string>

<string>云南</string>

<string>海南</string>

<string>新疆</string>

<string>西藏</string>

<string>臺灣</string>

<string>亞洲</string>

<string>歐洲</string>

<string>非洲</string>

<string>北美洲</string>

<string>南美洲</string>

<string>大洋洲</string>

</ArrayOfString>public

class

WebServiceHelper

{

//WSDL文檔中的命名空間

private

static

final

String

targetNameSpace="http://WebX/";

//WSDL文檔中的URL

private

static

final

String

WSDL="/WebServices/WeatherWebService.asmx?wsdl";

//需要調用的方法名(獲得本天氣預報Web

Services支持的洲、國內外省份和城市信息)

private

static

final

String

getSupportProvince="getSupportProvince";

//需要調用的方法名(獲得本天氣預報Web

Services支持的城市信息,根據(jù)省份查詢城市集合:帶參數(shù))

private

static

final

String

getSupportCity="getSupportCity";

//根據(jù)城市或地區(qū)名稱查詢獲得未來三天內天氣情況、現(xiàn)在的天氣實況、天氣和生活指數(shù)

private

static

final

String

getWeatherbyCityName="getWeatherbyCityName";

/********

*

獲得州,國內外省份和城市信息

*

@return

*/

public

List<String>

getProvince(){

List<String>

provinces=new

ArrayList<String>();

String

str="";

SoapObject

soapObject=new

SoapObject(targetNameSpace,getSupportProvince);

//request.addProperty("參數(shù)",

"參數(shù)值");調用的方法參數(shù)與參數(shù)值(根據(jù)具體需要可選可不選)

SoapSerializationEnvelope

envelope=new

SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet=true;

envelope.setOutputSoapObject(soapObject);//envelope.bodyOut=request;

AndroidHttpTransport

httpTranstation=new

AndroidHttpTransport(WSDL);

//或者HttpTransportSE

httpTranstation=new

HttpTransportSE(WSDL);

try

{

httpTranstation.call(targetNameSpace+getSupportProvince,

envelope);

SoapObject

result=(SoapObject)envelope.getResponse();

//下面對結果進行解析,結構類似json對象

//str=(String)

result.getProperty(6).toString();

int

count=result.getPropertyCount();

for(int

index=0;index<count;index++){

provinces.add(result.getProperty(index).toString());

}

}

catch

(IOException

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

catch

(XmlPullParserException

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

return

provinces;

}

/**********

*

根據(jù)省份或者直轄市獲取天氣預報所支持的城市集合

*

@param

province

*

@return

*/

public

List<String>

getCitys(String

province){

List<String>

citys=new

ArrayList<String>();

SoapObject

soapObject=new

SoapObject(targetNameSpace,getSupportCity);

soapObject.addProperty("byProvinceName",

province);

SoapSerializationEnvelope

envelope=new

SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet=true;

envelope.setOutputSoapObject(soapObject);

AndroidHttpTransport

httpTransport=new

AndroidHttpTransport(WSDL);

try

{

httpTransport.call(targetNameSpace+getSupportCity,

envelope);

SoapObject

result=(SoapObject)envelope.getResponse();

int

count=result.getPropertyCount();

for(int

index=0;index<count;index++){

citys.add(result.getProperty(index).toString());

}

}

catch

(IOException

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

catch

(XmlPullParserException

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

return

citys;

}

/***************************

*

根據(jù)城市信息獲取天氣預報信息

*

@param

city

*

@return

***************************/

public

WeatherBean

getWeatherByCity(String

city){

WeatherBean

bean=new

WeatherBean();

SoapObject

soapObject=new

SoapObject(targetNameSpace,getWeatherbyCityName);

soapObject.addProperty("theCityName",city);//調用的方法參數(shù)與參數(shù)值(根據(jù)具體需要可選可不選)

SoapSerializationEnvelope

envelope=new

SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet=true;

envelope.setOutputSoapObject(soapObject);//envelope.bodyOut=request;

AndroidHttpTransport

httpTranstation=new

AndroidHttpTransport(WSDL);

//或者HttpTransportSE

httpTranstation=new

HttpTransportSE(WSDL);

try

{

httpTranstation.call(targetNameSpace+getWeatherbyCityName,

envelope);

SoapObject

result=(SoapObject)envelope.getResponse();

//下面對結果進行解析,結構類似json對象

bean=parserWeather(result);

}

catch

(IOException

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

catch

(XmlPullParserException

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

return

bean;

}

/**

*

解析返回的結果

*

@param

soapObject

*/

protected

WeatherBean

parserWeather(SoapObject

soapObject){

WeatherBean

bean=new

WeatherBean();

List<Map<String,Object>>

list=new

ArrayList<Map<String,Object>>();

Map<String,Object>

map=new

HashMap<String,Object>();//城市名

bean.setCityName(soapObject.getProperty(1).toString());//城市簡介

bean.setCityDescription(soapObject.getProperty(soapObject.getPropertyCount()-1).toString());

bean.setLiveWeather(soapObject.getProperty(10).toString()+"\n"+soapObject.getProperty(11).toString());

//其他數(shù)據(jù)

//日期,

String

date=soapObject.getProperty(6).toString();

String

weatherToday="今天:"

+

date.split("

")[0];

weatherToday+="\n天氣:"+

date.split("

")[1];

weatherToday+="\n氣溫:"+soapObject.getProperty(5).toString();

weatherToday+="\n風力:"+soapObject.getProperty(7).toString();

weatherToday+="\n";

List<Integer>

icons=new

ArrayList<Integer>();

icons.add(parseIcon(soapObject.getProperty(8).toString()));

icons.add(parseIcon(soapObject.getProperty(9).toString()));

map.put("weatherDay",

weatherToday);

map.put("icons",icons);

list.add(map);

map=new

HashMap<String,Object>();

date=soapObject.getProperty(13).toString();

String

weatherTomorrow="明天:"

+

date.split("

")[0];

weatherTomorrow+="\n天氣:"+

date.split("

")[1];

weatherTomorrow+="\n氣溫:"+soapObject.getProperty(12).toString();

weatherTomorrow+="\n風力:"+soapObject.getProperty(14).toString();

weatherTomorrow+="\n";

icons=new

ArrayList<Integer>();

icons.add(parseIcon(soapObject.getProperty(15).toString()));

icons.add(parseIcon(soapObject.getProperty(16).toString()));

map.put("weatherDay",

weatherTomorrow);

map.put("icons",icons);

list.add(map);

map=new

HashMap<String,Object>();

date=soapObject.getProperty(18).toString();

String

weatherAfterTomorrow="后天:"

+

date.split("

")[0];

weatherAfterTomorrow+="\n天氣:"+

date.split("

")[1];

weatherAfterTomorrow

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論