android应用一(调用WebServices)

搞了一个月的android,现学现卖,终于还是搞完了,停下来,整理思路,写写记录吧。

我们知道android访问远程数据库主要有两种协议,一种是SOAP,另外一种就是HTTP。而我们再看看WebServices的请求方式。建立一个WebServices,直接在浏览器中查看,你会发现WebServices也同时提供了2中请求方式,也都是SOAP和HTTP-POST的方式。

是巧合还是必然,那都不重要了。重要的是android如何访问WebServices呢?

看下第一种SOAP访问的方式。

我们知道WebServices数据交互一般都是使用XML的格式,但是庞大的数据列表使用XML会消耗很大的流量,所有我们在安卓中的操作就使用了神器JSON格式来交互。。。

安卓SOAP访问WebServices

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访问WebServices

 

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访问的两种方式。

其实比对两种方式你会发现几乎都差不多,只是一些对象的使用不同罢了。。

本文从百度空间搬家到博客园。。

邮箱:yunanwu@foxmail.com 微博:@提灯寻影(http://weibo.com/wuyunnan) 技术主页:http://www.cnblogs.com/yuanawu/ 可以白手起家不可手无寸铁!我是我命运的主宰者,我是我灵魂的掌舵人! 每一次的选择都将是一个挑战!
原文地址:https://www.cnblogs.com/yunanwu/p/4168580.html