SOAP之WebService、JSON传值问题

2019年12月08日 阅读数:73
这篇文章主要向大家介绍SOAP之WebService、JSON传值问题,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

SOAP之WebService、JSON传值问题

发表于1年前(2012-08-17 15:11)   阅读( 1248) | 评论( 0)  0人收藏此文章, 我要收藏
0

首先,Server端(Java后台代码):(web.xml、sun-jaxws.xml、BulletinService.java、BulletinServiceDelegate.java)html

1 web.xml文件:

01 <?xml version="1.0" encoding="UTF-8"?>
02 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
03   <servlet>
04     <description>JAX-WS endpoint - UsersServiceService</description>
05     <display-name>UsersServiceService</display-name>
06     <servlet-name>UsersServiceService</servlet-name>
07     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
08     <load-on-startup>1</load-on-startup>
09   </servlet>
10  
11   <servlet-mapping>
12     <servlet-name>UsersServiceService</servlet-name>
13     <url-pattern>/BulletinServicePort</url-pattern>
14   </servlet-mapping>
15    
16   <welcome-file-list>
17     <welcome-file>index.jsp</welcome-file>
18   </welcome-file-list>
19   <listener>
20     <listener-class>
21     com.sun.xml.ws.transport.http.servlet.WSServletContextListener
22     </listener-class>
23   </listener>
24 </web-app>
1 sun-jaxws.xml文件,和web.xml同目录下
1 <?xml version = "1.0"?>
2 <endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
3     <endpoint name="BulletinServicePort" implementation="com.sk.service.BulletinService"
4         url-pattern="/BulletinServicePort">
5     </endpoint>
6 </endpoints>
1 BulletinService.java文件service层,去调用dao层的方法
01 package com.sk.service;
02  
03 import java.util.List;
04  
05 import javax.jws.WebMethod;
06 import javax.jws.WebService;
07  
08 import org.json.JSONArray;
09  
10 import com.sk.dao.IBulletinDao;
11 import com.sk.dao.impl.BulletinDaoImpl;
12 import com.sk.vo.Gpw_Bulletin_Info;
13  
14 <a href="http://my.oschina.net/u/142217" class="referer" target="_blank">@WebService</a>
15 public class BulletinService {
16  
17     IBulletinDao bulletinDao = new BulletinDaoImpl();
18  
19     @WebMethod
20     public String find() {
21         List<Gpw_Bulletin_Info> list = bulletinDao.findAll();
22         // 调用查询方法
23         JSONArray jsonArray = new JSONArray(list);
24         return jsonArray.toString();
25     }
26      
27 }
1 BulletinServiceDelegate.java文件service层,WebServices对外暴露数据
01 package com.sk.service;
02  
03 @javax.jws.WebService(targetNamespace = "http://service.sk.com/", serviceName = "BulletinServiceService", portName = "BulletinServicePort")
04 public class BulletinServiceDelegate {
05  
06     com.sk.service.BulletinService bulletinService = new com.sk.service.BulletinService();
07  
08     public String find() {
09         return bulletinService.find();
10     }
11 }

而后,Client端(Android代码):(Config.java、SOAPUtil.java、dbconfig.properties子三个文件完成Android客户端的WebServices部署)java

1 Config.java文件
01 package com.sk.soap;
02  
03 import java.io.IOException;
04 import java.util.Properties;
05  
06 public class Config {
07     private static Properties prop = new Properties();
08     static{
09         try {
10             //加载配置文件dbonfig.properties
11             prop.load(Config.class.getResourceAsStream("dbconfig.properties"));
12         catch (IOException e) {
13             // TODO Auto-generated catch block
14             e.printStackTrace();
15         }
16     }
17      
18     //获取dbconfig.properties中的值
19     public static final String WSDL_HTTP = prop.getProperty("WSDL_HTTP");
20      
21 }
1 SOAPUtil.java文件
01 package com.sk.soap;
02  
03 import java.io.IOException;
04  
05 import org.ksoap2.SoapEnvelope;
06 import org.ksoap2.serialization.SoapObject;
07 import org.ksoap2.serialization.SoapSerializationEnvelope;
08 import org.ksoap2.transport.HttpTransportSE;
09 import org.xmlpull.v1.XmlPullParserException;
10  
11 import android.util.Log;
12          
13 public class SOAPUtil {
14  
15     public static Object TransportData(final String service, final String webMethod,Object[] params){
16         SoapObject request = new SoapObject("http://service.sk.com/", webMethod);      
17         for(int i=0;i<params.length;i++){
18             Log.v("params", params[i].toString());
19             request.addProperty("arg"+i,params[i]);
20         }      
21         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);        
22         envelope.bodyOut = request;        
23         HttpTransportSE ht = new HttpTransportSE(Config.WSDL_HTTP+service);
24         try {          
25             ht.call(null, envelope);               
26             if (envelope.getResponse() != null) {              
27                 return envelope.getResponse();
28             }      
29         catch (IOException e) {
30             e.printStackTrace();                   
31         catch (XmlPullParserException e) {
32             e.printStackTrace();                   
33         }      
34         return null;
35     }
36      
37 }
1 dbconfig.properties文件,写连接地址
1 WSDL_HTTP=http\://192.168.0.91\:8080/SK_Java_Service/
最后看一下Activity中如何经过JOSN取后台传过来的值:    
1 在oncreate()方法里边调用下边方法jsonAdapter()给适配器
1 List<Gpw_Bulletin_Info> list = this.jsonAdapter();
2 // 加载适配器
3 ListAdapter adapter = new ListAdapter(ListActivity.this, list);
4  
5 list_LV.setAdapter(adapter);
01 private List<Gpw_Bulletin_Info> jsonAdapter() {
02         List<Gpw_Bulletin_Info> list = new ArrayList<Gpw_Bulletin_Info>();
03  
04         Object obj = SOAPUtil.TransportData("BulletinServicePort?wsdl""find",
05                 new Object[] {});
06         String data = String.valueOf(obj.toString());
07         Log.i("TAG""data=" + data);
08         if (data != null) {
09             try {
10                 JSONArray json = new JSONArray(data);
11  
12                 for (int i = 0; i < json.length(); i++) {
13                     Gpw_Bulletin_Info info = new Gpw_Bulletin_Info();
14  
15                     info.setBulletinid(((JSONObject) json.get(i))
16                             .getInt("bulletinid"));
17                     info.setTitle(((JSONObject) json.get(i)).getString("title"));
18                     info.setHtmlcontent(((JSONObject) json.get(i))
19                             .getString("htmlcontent"));
20                     info.setStartdate(((JSONObject) json.get(i))
21                             .getString("startdate"));
22  
23                     list.add(info);
24                 }
25             catch (JSONException e) {
26                 e.printStackTrace();
27             }
28         else {
29             Toast.makeText(getApplicationContext(), "暂无数据!", Toast.LENGTH_SHORT)
30                     .show();
31         }
32         return list;
33     }