服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android调用WebService实例分析

android调用WebService实例分析

2021-04-02 17:00天高空 Android

这篇文章主要介绍了android调用WebService的方法,以实例形式较为详细的分析了WebService的调用原理与具体使用方法,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了android调用WebService的方法。分享给大家供大家参考。具体如下:

WebService是一种基于SOAP协议的远程调用标准,通过webservice可以将不同操作系统平台、不同语言、不同技术整合到一块。在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方的SDK来调用WebService。PC版本的WEbservice客户端库非常丰富,例如Axis2,CXF等,但这些开发包对于Android系统过于庞大,也未必很容易移植到Android系统中。因此,这些开发包并不是在我们的考虑范围内。适合手机的WebService客户端的SDK有一些,比较常用的有Ksoap2,可以从http://code.google.com/p/ksoap2-android/downloads/list进行下载;将下载的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包复制到Eclipse工程的lib目录中,当然也可以放在其他的目录里。同时在Eclipse工程中引用这个jar包。

java代码如下:

?
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.arg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class CallWebServiceActivity extends Activity {
  //显示结果的listview
  ListView listView=null;
  //输入文本框
  EditText provinceEdit=null;
  //用于存放数据的集合list
  List<Map<String, Object>> data=null;
  //提示对话框
  ProgressDialog myDialog=null;
  //搜索按钮
  Button searchButton=null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //获得文本输入框
  provinceEdit=(EditText) this.findViewById(R.id.provinceEdit);
  //获得搜索按钮
  searchButton=(Button) this.findViewById(R.id.searchButton);
  //为搜索按钮添加单击监听事件
  searchButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
     //响应按钮单击事件的函数
     ResponseOnClick();
    }
  });
 }
 //响应按钮单击事件的函数
 public void ResponseOnClick(){
  //创建一个线程
  HttpThread thread=new HttpThread(handler);
  //构造请求参数
  HashMap <String ,Object> params=new HashMap<String ,Object>();
  try{
   CharSequence etValue=provinceEdit.getText();
   String name="";
   if(etValue!=null){
    //字符转码
    name=new String(etValue.toString().getBytes(),"UTF-8");
   }
   params.put("byProvinceName", name);
  }catch(Exception ex){
   ex.printStackTrace();
  }
  //
  String url="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
  // String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
  String nameSpace = "http://WebXml.com.cn/";
  String methodName = "getSupportCity";
  // 开始新线程进行WebService请求
  thread.doStart(url, nameSpace, methodName, params);
 }
 /**
 * 捕获消息队列
 *
 */
 Handler handler=new Handler(){
  public void handleMessage(Message m){
   ArrayList <String> myList=(ArrayList<String>)m.getData().getStringArrayList("data");
   if(myList !=null){
    if(data !=null){
     data.clear();
    }else{
     data=new ArrayList<Map <String, Object>>();
    }
    for(int i=0;i<myList.size();i++){
     Map<String, Object> item=new HashMap<String, Object>();
     item.put("text", myList.get(i));
     data.add(item);
    }
    /**
    * 列表显示
    *
    */
    SimpleAdapter simpleAdapter=new SimpleAdapter(CallWebServiceActivity.this
    ,data,R.layout.listlayout,new String[] {"text"},new int []{R.id.showData});
    listView=(ListView) findViewById(R.id.showListView);
    listView.setAdapter(simpleAdapter);
   }
  }
 };
 /**
 * 线程类
 * @author Administrator
 *
 */
 public class HttpThread extends Thread{
  private Handler handle=null;
  String url=null;
  String nameSpace=null;
  String methodName=null;
  HashMap <String ,Object> params=null;
  ProgressDialog progressDialog=null;
  //构造函数
  public HttpThread(Handler hander){
   handle=hander;
  }
  /**
  * 启动线程
  */
  public void doStart(String url, String nameSpace, String methodName,
     HashMap<String, Object> params) {
    // TODO Auto-generated method stub
   this.url=url;
   this.nameSpace=nameSpace;
   this.methodName=methodName;
   this.params=params;
   progressDialog=ProgressDialog.show(CallWebServiceActivity.this, "提示","正在请求请稍等……", true);
   this.start();
   }
  /**
  * 线程运行
  */
  @Override
  public void run() {
   // TODO Auto-generated method stub
   System.out.println("jack");
   super.run();
   try{
    //web service请求
    SoapObject result=(SoapObject) CallWebService();
    //构造数据
    ArrayList<String> list=null;
    if(result !=null && result.getPropertyCount() > 0){
     list=new ArrayList<String>();
     for(int i=0;i<result.getPropertyCount();i++){
      SoapPrimitive value=(SoapPrimitive) result.getProperty(i);
      list.add(value.toString());
     }
     //a取消进度对话框
     progressDialog.dismiss();
     //构造消息
     Message message=handle.obtainMessage();
     Bundle b=new Bundle();
     b.putStringArrayList("data", list);
     message.setData(b);
     handle.sendMessage(message);
    }
   }catch(Exception ex){
    ex.printStackTrace();
   }finally{
   }
  }
  /**
  * 请求web service
  */
  protected Object CallWebService(){
   String SOAP_ACTION = nameSpace + methodName;
   //创建SoapObject实例
   SoapObject request=new SoapObject(nameSpace,methodName);
   //生成调用web service方法的soap请求消息
   SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
   //设置.net web service
   envelope.dotNet=true;
   //发送请求
   envelope.setOutputSoapObject(request);
   //请求参数
   if(params != null && !params.isEmpty() ){
    for(Iterator it=params.entrySet().iterator();it.hasNext();){
     Map.Entry e=(Entry) it.next();
     request.addProperty(e.getKey().toString(),e.getValue());
    }
   }
   //
   AndroidHttpTransport androidHttpTrandsport=new AndroidHttpTransport(url);
   SoapObject result=null;
   try{
    //web service请求
    androidHttpTrandsport.call(SOAP_ACTION, envelope);
    //得到返回结果
    result=(SoapObject) envelope.getResponse();
   }catch(Exception ex){
    ex.printStackTrace();
   }
   return result;
  }
 }
}

希望本文所述对大家的Android程序设计有所帮助。

延伸 · 阅读

精彩推荐