Android中调用已经部署到IIS中.net的webservice数据Android端无法执行报错的问题。求大神指点。
今天尝试做的一个简单webservice,我已经部署到IIS中去了。服务器链接路径是http://192.168.127.105/WebService1.asmx?wsdl。
Android中我写的调用.net的webservice的源码
1 /**
2 *
3 *
4 * @author wym
5 *
6 */
7 public class MainActivity extends Activity {
8
9 final String METHOD_HELLO_WORLD = "HelloWorld";
10 final String METHOD_ECHO_MESSAGE = "EchoMessage";
11 private TextView txtMsg;
12 //服务器链接
13 final String WEB_SERVICE_URL = "http://192.168.127.105/WebService1.asmx?wsdl";
14 //默认命名空间
15 final String Namespace = "http://tempuri.org/";
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19 txtMsg=(TextView) findViewById(R.id.txtMsg);
20 }
21
22 //调用dohello方法
23 public void doHellow(View view)
24 {
25 Log.i("---------doHellow---", "doHellow");
26 Map<String, String> values = new HashMap<String, String>();
27 values.put("msg", "这是Android手机doHellow方法发出的信息");
28 Request(METHOD_HELLO_WORLD);
29 }
//调用doEchoMessage方法
30 public void doEchoMessage(View view)
31 {
32 Log.i("---------doEchoMessage---", "doEchoMessage");
33 Map<String, String> values = new HashMap<String, String>();
34 values.put("msg", "这是Android手机doEchoMessage方法发出的信息");
35 Request(METHOD_ECHO_MESSAGE, values);
36 }
37
38 /**
39 * 执行异步任务
40 *
41 * @param params
42 * 方法名+参数列表(哈希表形式)
43 */
44 public void Request(Object... params) {
45 new AsyncTask<Object, Object, String>() {
46
47 @Override
48 protected String doInBackground(Object... params) {
49 if (params != null && params.length == 2) {
50 return CallWebService((String) params[0],
51 (Map<String, String>) params[1]);
52 } else if (params != null && params.length == 1) {
53 return CallWebService((String) params[0], null);
54 } else {
55 return null;
56 }
57 }
58
59 protected void onPostExecute(String result) {
60 if (result != null) {
61 txtMsg.setText("服务器回复的信息 : " + result);
62 }
63 };
64
65 }.execute(params);
66 }
67
68 /**
69 *
70 * 调用WebService
71 * @return WebService 返回值
72 */
73 public String CallWebService(String MethodName, Map<String, String> params) {
74 Log.i("--------进入调用CallWebService------","CallWebService");
75 //1、指定webservice的命名空间和调用的方法名
76 //SoapObject类的第一个参数表示WebService的命名空间,
77 //可以从WSDL文档中找到WebService的命名空间。第二个参数表示要调用的WebService方法名。
78 SoapObject request=new SoapObject(Namespace,MethodName);
79
80
81 // 2、设置调用方法的参数值,如果没有参数,可以省略,
82 if(params!=null){
83 Iterator iter=params.entrySet().iterator();
84 while(iter.hasNext()){
85 Map.Entry entry=(Map.Entry) iter.next();
86 request.addProperty((String) entry.getKey(),
87 (String) entry.getValue());
88 }
89 }
90 // 3、生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
91 //SOAP协议的版本号可以从WebService的WSDL文档中查看到
92 SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER12);
93 envelope.bodyOut = request;
94 // c#写的应用程序必须加上这句
95 envelope.dotNet = true;
96 //创建HttpTransportsSE对象。通过HttpTransportsSE类的构造方法可以指定WebService的WSDL文档的URL:
97 HttpTransportSE ht=new HttpTransportSE(WEB_SERVICE_URL);
98 // 使用call方法调用WebService方法
99 try {
100 //Call方法的第一个参数一般为null,第2个参数就是在第3步创建的SoapSerializationEnvelope对象。
101 ht.call(null, envelope);
102 } catch (HttpResponseException e) {
103 e.printStackTrace();
104 } catch (IOException e) {
105 e.printStackTrace();
106 } catch (XmlPullParserException e) {
107 e.printStackTrace();
108 }
109 //使用envelope对象的getResponse方法获得WebService方法的返回结果
110 try {
111 SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
112 if(result!=null){
113 Log.i("--------收到的回复------",result+"");
114 return result.toString();
115 }
116 } catch (SoapFault e) {
117 Log.e("---------发生了错误---------", e.getMessage().toString());
118 e.printStackTrace();
119 }
120 return null;
121 }
122
123 }当我在app中点击执行调用doHellow方法或者调用doEchoMessage方法时候程序报错,原因貌似是说在调用webService的时候异步线程中执行有误。报错截图如下:


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4798/viewspace-3160029/ ,如需转载,请注明出处,否则将追究法律责任。