真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android中怎么與服務(wù)器端數(shù)據(jù)進行交互

今天就跟大家聊聊有關(guān)Android中怎么與服務(wù)器端數(shù)據(jù)進行交互,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

在保亭黎族等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站 網(wǎng)站設(shè)計制作按需求定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),保亭黎族網(wǎng)站建設(shè)費用合理。

?首先下載KSOAP包:

ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar

然后新建android項目:并把下載的KSOAP包放在android項目的lib目錄下:右鍵->build path->configure build path--選擇Libraries,如圖:

Android中怎么與服務(wù)器端數(shù)據(jù)進行交互

以下分為七個步驟來調(diào)用WebService方法:

1、實例化SoapObject 對象,指定webService的命名空間(從相關(guān)WSDL文檔中可以查看命名空間),以及調(diào)用方法名稱。如:

//命名空間          private static final String serviceNameSpace="http://WebXml.com.cn/";      //調(diào)用方法(獲得支持的城市)         private static final String getSupportCity="getSupportCity";    //實例化SoapObject對象             SoapObject request=new SoapObject(serviceNameSpace, getSupportCity);

2、假設(shè)方法有參數(shù)的話,設(shè)置調(diào)用方法參數(shù)

request.addProperty("參數(shù)名稱","參數(shù)值");

3、設(shè)置SOAP請求信息(參數(shù)部分為SOAP協(xié)議版本號,與你要調(diào)用的webService中版本號一致):

//獲得序列化的Envelope             SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);      envelope.bodyOut=request;

4、注冊Envelope,

?(new MarshalBase64()).register(envelope);

5、構(gòu)建傳輸對象,并指明WSDL文檔URL:

//請求URL         private static final String serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";  //Android傳輸對象                 AndroidHttpTransport transport=new  AndroidHttpTransport(serviceURL);        transport.debug=true;

6、調(diào)用WebService(其中參數(shù)為1:命名空間+方法名稱,2:Envelope對象):

transport.call(serviceNameSpace+getWeatherbyCityName, envelope);

7、解析返回數(shù)據(jù):

if(envelope.getResponse()!=null){          return parse(envelope.bodyIn.toString());     }  /**************   * 解析XML       * @param str   * @return  */      private static List parse(String str){      String temp;             List list=new ArrayList();     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

這樣就成功啦。那么現(xiàn)在我們就來測試下吧,這里有個地址提供webService天氣預(yù)報的服務(wù)的,我這里只提供獲取城市列表:

//命名空間      private static final String serviceNameSpace="http://WebXml.com.cn/";      //請求URL      private static final String serviceURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";      //調(diào)用方法(獲得支持的城市)      private static final String getSupportCity="getSupportCity";      //調(diào)用城市的方法(需要帶參數(shù))      private static final String getWeatherbyCityName="getWeatherbyCityName";      //調(diào)用省或者直轄市的方法(獲得支持的省份或直轄市)      private static final String getSupportProvince="getSupportProvince";

我們選擇獲取國內(nèi)外主要城市或者省份的方法吧:getSupportProvice,然后調(diào)用,你會發(fā)現(xiàn)瀏覽器返回給我們的是xml文檔:

     直轄市     特別行政區(qū)     黑龍江     吉林     遼寧    內(nèi)蒙古     河北     河南     山東     山西     江蘇     安徽     陜西     寧夏     甘肅     青海     湖北     湖南     浙江     江西     福建     貴州     四川     廣東     廣西    云南     海南     新疆     西藏     臺灣     亞洲     歐洲     非洲     北美洲     南美洲     大洋洲     

我們可以用 listview來顯示:

那么下面我將給出全部代碼:

public class WebServiceHelper {     //WSDL文檔中的命名空間     private static final   String targetNameSpace="http://WebXml.com.cn/";    //WSDL文檔中的URL      private static final   String WSDL="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";             //需要調(diào)用的方法名(獲得本天氣預(yù)報Web Services支持的洲、國內(nèi)外省份和城市信息)      private static final String getSupportProvince="getSupportProvince";      //需要調(diào)用的方法名(獲得本天氣預(yù)報Web Services支持的城市信息,根據(jù)省份查詢城市集合:帶參數(shù))     private static final String getSupportCity="getSupportCity";      //根據(jù)城市或地區(qū)名稱查詢獲得未來三天內(nèi)天氣情況、現(xiàn)在的天氣實況、天氣和生活指數(shù)      private static final String getWeatherbyCityName="getWeatherbyCityName";      /********       * 獲得州,國內(nèi)外省份和城市信息      * @return       */      public  List getProvince(){          List   provinces=new ArrayList();          String str="";          SoapObject soapObject=new SoapObject(targetNameSpace,getSupportProvince);          //request.addProperty("參數(shù)", "參數(shù)值");調(diào)用的方法參數(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();              //下面對結(jié)果進行解析,結(jié)構(gòu)類似json對象              //str=(String) result.getProperty(6).toString();                          int count=result.getPropertyCount();              for(int index=0;index getCitys(String province){          List citys=new ArrayList();          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> list=new ArrayList>();   Map map=new HashMap();//城市名          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風(fēng)力:"+soapObject.getProperty(7).toString();          weatherToday+="\n";    List icons=new ArrayList();              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();           date=soapObject.getProperty(13).toString();          String weatherTomorrow="明天:" + date.split(" ")[0];            weatherTomorrow+="\n天氣:"+ date.split(" ")[1];           weatherTomorrow+="\n氣溫:"+soapObject.getProperty(12).toString();          weatherTomorrow+="\n風(fēng)力:"+soapObject.getProperty(14).toString();          weatherTomorrow+="\n";                  icons=new ArrayList();                   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();                   date=soapObject.getProperty(18).toString();          String weatherAfterTomorrow="后天:" + date.split(" ")[0];            weatherAfterTomorrow+="\n天氣:"+ date.split(" ")[1];           weatherAfterTomorrow+="\n氣溫:"+soapObject.getProperty(17).toString();          weatherAfterTomorrow+="\n風(fēng)力:"+soapObject.getProperty(19).toString();          weatherAfterTomorrow+="\n";                  icons=new ArrayList();          icons.add(parseIcon(soapObject.getProperty(20).toString()));                icons.add(parseIcon(soapObject.getProperty(21).toString()));                  map.put("weatherDay", weatherAfterTomorrow);          map.put("icons",icons);          list.add(map);        bean.setList(list);          return bean;      }         //解析圖標(biāo)字符串       private int parseIcon(String data){          // 0.gif,返回名稱0,           int resID=32;           String result=data.substring(0, data.length()-4).trim();            // String []icon=data.split(".");            // String result=icon[0].trim();            //   Log.e("this is the icon", result.trim());                       if(!result.equals("nothing")){                 resID=Integer.parseInt(result.trim());             }         return resID;           //return ("a_"+data).split(".")[0];       }  }

上就是我所作的查詢天氣預(yù)報的全部核心代碼了,讀者可以根據(jù)注釋以及本文章了解下具體實現(xiàn),相信很快就搞明白了,運行結(jié)果如下:

Android中怎么與服務(wù)器端數(shù)據(jù)進行交互

看完上述內(nèi)容,你們對Android中怎么與服務(wù)器端數(shù)據(jù)進行交互有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。


本文標(biāo)題:Android中怎么與服務(wù)器端數(shù)據(jù)進行交互
URL地址:http://weahome.cn/article/gdsjjc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部