SOAP之WebService、JSON传值问题

SOAP之WebService、JSON传值问题

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

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

1 web.xml文件:

01 "1.0" encoding="UTF-8"?>
02 "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   
04     JAX-WS endpoint - UsersServiceService
05     UsersServiceService
06     UsersServiceService
07     class>com.sun.xml.ws.transport.http.servlet.WSServletclass>
08     1
09   
10  
11   
12     UsersServiceService
13     /BulletinServicePort
14   
15    
16   
17     index.jsp
18   
19   
20     class>
21     com.sun.xml.ws.transport.http.servlet.WSServletContextListener
22     class>
23   
24
1 sun-jaxws.xml文件,和web.xml同目录下
1 "1.0"?>
2 "2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
3     "BulletinServicePort" implementation="com.sk.service.BulletinService"
4         url-pattern="/BulletinServicePort">
5     
6
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  
15 public class BulletinService {
16  
17     IBulletinDao bulletinDao = new BulletinDaoImpl();
18  
19     @WebMethod
20     public String find() {
21         List 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部署)

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
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 list = this.jsonAdapter();
2 // 加载适配器
3 ListAdapter adapter = new ListAdapter(ListActivity.this, list);
4  
5 list_LV.setAdapter(adapter);
01 private List jsonAdapter() {
02         List list = new ArrayList();
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     }

你可能感兴趣的:(java)