package com.newelite.netlibrary;
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 org.ksoap2.transport.HttpTransportSE;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class WebControl{
//用于存放数据的集合list
List<Map<String, Object>> data=null;
//响应按钮单击事件的函数
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 = "getSupportProvince ";//"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);
}
}
};
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) {
this.url=url;
this.nameSpace=nameSpace;
this.methodName=methodName;
this.params=params;
//progressDialog=ProgressDialog.show(MainActivity.this, "提示","正在请求请稍等......", true);
this.start();
}
/**
* 线程运行
*/
@Override
public void run() {
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=false;
//发送请求
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;
}
}
public String GetWebServiceResult(){
String url="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
String nameSpace = "http://WebXml.com.cn/";
String methodName = "getSupportProvince ";//"getSupportCity"; http://WebXml.com.cn/getSupportProvince
String strResult1="";
try
{
//web service请求
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());
// }
// }
//
// HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
//
// try {
// androidHttpTransport.call(SOAP_ACTION, envelope);
//
// SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
// //SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
// strResult1=resultsRequestSOAP.toString();
// strResult1="22";
// } catch (Exception e) {
// return "Get Respone Error:"+e.toString();
// }
AndroidHttpTransport androidHttpTrandsport=new AndroidHttpTransport(url);
SoapObject result=null;
try{
//web service请求
androidHttpTrandsport.call(SOAP_ACTION, envelope);
//得到返回结果
result=(SoapObject) envelope.getResponse();
}catch(Exception ex1){
return ex1.toString();
// ex.printStackTrace();
}
strResult1=result.toString();
// return strReturn;
}
catch(Exception ex)
{
return "Error2:"+ex.toString();
}
return strResult1;
}
}
以前用2.2 访问 donet webService没有问题,到3.0上访问出现android.os.NetworkOnMainThreadException
找了些资料经过实践
似乎是3.0在网络上做了更加严格的限制,更多的查询API上的StrictMode 类
java.net.Unknownhostexception
<uses-permission android:name="android.permission.INTERNET" />