91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中怎么與服務器端數據進行交互

發布時間:2021-07-20 14:53:41 來源:億速云 閱讀:161 作者:Leah 欄目:移動開發

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

?首先下載KSOAP包:

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

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

Android中怎么與服務器端數據進行交互

以下分為七個步驟來調用WebService方法:

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

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

2、假設方法有參數的話,設置調用方法參數

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

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

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

4、注冊Envelope,

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

5、構建傳輸對象,并指明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、調用WebService(其中參數為1:命名空間+方法名稱,2:Envelope對象):

transport.call(serviceNameSpace+getWeatherbyCityName, envelope);

7、解析返回數據:

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;  }

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

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

我們選擇獲取國內外主要城市或者省份的方法吧:getSupportProvice,然后調用,你會發現瀏覽器返回給我們的是xml文檔:

<?xml version="1.0" encoding="utf-8" ?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns="http://WebXml.com.cn/">    <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>南美洲</string>     <string>大洋洲</string>     </ArrayOfString>

我們可以用 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";             //需要調用的方法名(獲得本天氣預報Web Services支持的洲、國內外省份和城市信息)      private static final String getSupportProvince="getSupportProvince";      //需要調用的方法名(獲得本天氣預報Web Services支持的城市信息,根據省份查詢城市集合:帶參數)     private static final String getSupportCity="getSupportCity";      //根據城市或地區名稱查詢獲得未來三天內天氣情況、現在的天氣實況、天氣和生活指數      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("參數", "參數值");調用的方法參數與參數值(根據具體需要可選可不選)                  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;      }          /**********       * 根據省份或者直轄市獲取天氣預報所支持的城市集合       * @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;      }          /***************************       * 根據城市信息獲取天氣預報信息       * @param city       * @return       ***************************/     public  WeatherBean getWeatherByCity(String city){                  WeatherBean bean=new WeatherBean();          SoapObject soapObject=new SoapObject(targetNameSpace,getWeatherbyCityName);          soapObject.addProperty("theCityName",city);//調用的方法參數與參數值(根據具體需要可選可不選)                  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()); //其他數據 //日期,          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+="\n氣溫:"+soapObject.getProperty(17).toString();          weatherAfterTomorrow+="\n風力:"+soapObject.getProperty(19).toString();          weatherAfterTomorrow+="\n";                  icons=new ArrayList<Integer>();          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;      }         //解析圖標字符串       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];       }  }

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

Android中怎么與服務器端數據進行交互

看完上述內容,你們對Android中怎么與服務器端數據進行交互有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

诸暨市| 广东省| 来宾市| 游戏| 富顺县| 沙湾县| 三台县| 重庆市| 当雄县| 南阳市| 林甸县| 福海县| 象山县| 新龙县| 峡江县| 洪洞县| 雅安市| 巨鹿县| 莱西市| 岫岩| 西乌珠穆沁旗| 琼结县| 景宁| 彰武县| 北碚区| 新营市| 云林县| 阿拉善盟| 正镶白旗| 射阳县| 沭阳县| 商都县| 准格尔旗| 藁城市| 叶城县| 镇远县| 德安县| 大宁县| 丹寨县| 岳阳市| 清徐县|