android應用一(調用WebServices)

搞了一個月的android,現學現賣,終於仍是搞完了,停下來,整理思路,寫寫記錄吧。java

咱們知道android訪問遠程數據庫主要有兩種協議,一種是SOAP,另一種就是HTTP。而咱們再看看WebServices的請求方式。創建一個WebServices,直接在瀏覽器中查看,你會發現WebServices也同時提供了2中請求方式,也都是SOAP和HTTP-POST的方式。android

 

 

是巧合仍是必然,那都不重要了。重要的是android如何訪問WebServices呢?數據庫

看下第一種SOAP訪問的方式。json

咱們知道WebServices數據交互通常都是使用XML的格式,可是龐大的數據列表使用XML會消耗很大的流量,全部咱們在安卓中的操做就使用了神器JSON格式來交互。。。瀏覽器

安卓SOAP訪問WebServicesthis

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
public Boolean SoapRequestWeb()
    {
         String nameSpace =ConfigCommon.getNetConfigProperties().getProperty( "nameSpace" ); //WebServices命名空間
         String serviceURL = ConfigCommon.getNetConfigProperties().getProperty( "serviceURL" );; //請求地址
         String methodName = "Login" ; //方法名
         String soapAction = "http://tempuri.org/Login" ;//處理動做
         SoapObject request = new SoapObject(nameSpace, methodName); //聲明SOAP對象
         //請求參數
         request.addProperty( "DeviceID" ,iemi);    
                 request.addProperty( "phone" ,phone);
                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //生成調用Webservice方法的SOAP請求信息
                 envelope.bodyOut = request;
                 envelope.dotNet= true ; //指定是否爲.NET版本
                 //建立HttpTransportSE對象。經過HttpTransportSE類的構造方法能夠指定WebService的WSDL文檔的URL
                 HttpTransportSE ht = new HttpTransportSE(serviceURL);
                 ht.debug = true ;
                 String result= "" ;
             try {
                         ht.call(soapAction, envelope); //使用call方法調用WebService方法,請求WebServices
                         if (envelope.getResponse() != null ) {
                              result= envelope.getResponse().toString(); //獲取輸出結果
                         if (result.isEmpty() || result.equals( "null" ))
                              {
                             // Toast.makeText(this,"不存在該用戶,請重試!",Toast.LENGTH_LONG).show();
                             //Message("不存在該賬號,請確保手機號正確");
                              return false ;
                          }
                     //使用Json對象對獲取的數據進行解析。
                     JSONObject  jsonObj= new JSONObject(result);
                     String name=jsonObj.getString( "UserName" );
                    //保存當前登錄用戶
                     T_Users tu= new T_Users();
                     tu.setUserName(name);
                     if (!name.isEmpty())
                         {
                         //保存好信息
                         // setContentView(R.layout.index);               
                        return true ;
                         }
                 else
                     {
                         Toast.makeText( this ,result.toString(),Toast.LENGTH_LONG).show();
                     return false ;
                     }
                 } else {
                    return false ;
                 }
             } catch (Exception e) {
                 e.printStackTrace();
                 Toast.makeText( this , e.getMessage(),Toast.LENGTH_LONG).show();
                 return false ;
             }
    }
 

 

安卓HTTP-POST訪問WebServicesspa

 

 

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
public void HttpRequestWeb()
     {
          String serviceURL = ConfigCommon.getNetConfigProperties().getProperty( "serviceURL" );; //請求地址
          String uriAPI = serviceURL+ "/ApplyOut" ;
              //創建HTTP Post連線/
             HttpPost httpRequest = new HttpPost(uriAPI);
             List params= new ArrayList();  //參數列表
             params.add( new BasicNameValuePair( "rid" ,ApplyOutRowid));  //添加參數
         try
         {
                 //設置Http請求實體
              httpRequest.setEntity( new UrlEncodedFormEntity(params,HTTP.UTF_8));
                  /*取得HTTP 輸出對象*/
                  HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
                  if(httpResponse.getStatusLine().getStatusCode() == 200)  
                     
                        /*取出響應字符串*/
                        String strResult = EntityUtils.toString(httpResponse.getEntity());
                        int index=strResult.lastIndexOf( "\">" );
                        strResult=  strResult.substring(index+ 2 , index+ 6 );
                        Common.Message(strResult, this );
                        //若是正確入住,則跳轉到主界面
                        if (strResult.equals( "true" ))
                        {
                        Common.Message( "已離開" , this );
                        this .finish();
                        }
                    else
                        {
                        Common.Message( "暫時沒法離開,請重試!" , this );
                        }         
                 
                  else 
                     
                       String error= "Error Response: " +httpResponse.getStatusLine().toString();
                       Common.Message(error, this );
                     
             }
         catch (Exception e)
         {
              Common.Message(e.getMessage(), this );
         }
     }
 

 

這就是ANDROID訪問WebServcies的兩種方式,這也是JAVA訪問的兩種方式。debug

其實比對兩種方式你會發現幾乎都差很少,只是一些對象的使用不一樣罷了。。code

本文從百度空間搬家到博客園。。orm

相關文章
相關標籤/搜索