Android 调用webservice

这里使用的是ksoap2的webservice工具.

下载地址如下:

http://ksoap2-android.googlecode.com/files/ksoap2-android-assembly-2.4-jar-with-dependencies.jar

 Url指的是你的webservice的地址.一般都是以***.wsdl或者***.?wsdl结束的...但是.需要注意的是..要去掉后面的.wsdl或者.?wsdl

代码
 1 public class TestWSDL extends Activity {
 2     /** Called when the activity is first created. */
 3     //方法名称.
 4     private static final String METHOD_NAME = "loadMyGroups";
 5     // 这里的命名空间一定要看到wsdl文件后才定,不然会出错
 6     private static final String NAMESPACE = "http://yournamespace/";
 7 
 8     private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
 9    
10     private static final String URL = "http://192.168.0.1:8080/yoncserver/";
11 
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16         Log.v("gtrgtr""envelope.getResponse()=============");
17         try {
18             SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
19             // request.addProperty("theCityCode", "2011");
20             // request.addProperty("credentials", new UserCredentials(
21             // "mak", "mak"));
22             // webservice 传入对象对象方式调用
23             PropertyInfo pi = new PropertyInfo();
24             UserCredentials u = new UserCredentials("mak""mak");
25             pi.setName("credentials"); //传入的对象名..
26             pi.setValue(u);
27             pi.setType(u.getClass());
28 
29             request.addProperty(pi);
30 
31             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
32                     SoapEnvelope.VER11);
33             // envelope.setOutputSoapObject(request);
34             // String uur = "?r=" + new Date().getTime();
35             // uur = Base64.encode(URL.getBytes()) + uur;
36             // uur = URL + uur;
37             // Log.v("gtrgtr", "uur========" + uur);
38             envelope.dotNet = true;
39             envelope.setOutputSoapObject(request);
40             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
41             // envelope.bodyOut = request;
42             androidHttpTransport.call(null, envelope);
43             Object result = envelope.getResponse();// String [] str =
44                                                     // (String
45                                                     // [])result;Log.d("result",result.toString());edit1.setText(result.toString());//handle
46                                                     // result herecatch
47                                                     // (Exception e)
48                                                     // {e.printStackTrace();}
49 
50             Log.v("gtrgtr""result=========" + result);
51         } catch (Exception e) {
52             e.printStackTrace();
53         }
54     }
55 }

这里是我们要像 webservice里传入的对象.需要序列化.实现KvmSerializable.

对象
 1 package com.gtrgtr.test.wsdl;
 2 
 3 import java.util.Hashtable;
 4 
 5 import org.ksoap2.serialization.KvmSerializable;
 6 import org.ksoap2.serialization.PropertyInfo;
 7 
 8 public class UserCredentials implements KvmSerializable {
 9 
10     /**
11      * 
12      */
13     private String login;
14     private String password;
15 
16     public UserCredentials() {
17         
18     }
19     
20     public UserCredentials(String login, String password) {
21         this.login = login;
22         this.password = password;
23     }
24 
25     public Object getProperty(int arg0) {
26         switch (arg0) {
27         case 0:
28             return login;
29         case 1:
30             return password;
31         }
32         return null;
33 
34     }
35 
36     public int getPropertyCount() {
37         // TODO Auto-generated method stub
38         return 2;
39     }
40 
41     public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
42         switch (arg0) {
43         case 0:
44             arg2.type = PropertyInfo.STRING_CLASS;
45             arg2.name = "login";
46             break;
47         case 1:
48             arg2.type = PropertyInfo.STRING_CLASS;
49             arg2.name = "password";
50             break;
51         default:
52             break;
53         }
54 
55     }
56 
57     public void setProperty(int arg0, Object arg1) {
58         switch (arg0) {
59         case 0:
60             login = arg1.toString();
61             break;
62         case 1:
63             password = arg1.toString();
64             break;
65         default:
66             break;
67         }
68 
69     }
70 
71 }
72 
原文地址:https://www.cnblogs.com/qwhg/p/1878724.html