利用J2SE的ksoap2標準,做一個android連接webservice。
因爲soap封裝的關係,android application在接收到數據後不能夠正確的按照J2SE的標準來獲取。
在運用之前,我們先要引導兩個jar進入工程的buildpath
commons-httpclient.jar
ksoap2-android-assembly-2.4-jar-with-dependencies.jar
這兩個jar包都可以在網上查到下載,引導完後再做一項準備工作。
webservice接口:http://192.168.0.2:8080/axis2/services/Manager?wsdl
在android當中,不能寫localhost,必須寫清楚PC機當前的網絡IP
在文件:AndroidManifest.xml放入:
<uses-permission android:name="android.permission.INTERNET"/>
天氣預報WebService地址:http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx
package gcy.android;
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 org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
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 MainActivity 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(MainActivity.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(MainActivity.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;
}
}
}