SOAP之WebService、JSON传值问题

来源:互联网 发布:淘宝联盟里的优惠卷 编辑:程序博客网 时间:2024/05/01 14:11

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)

1web.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>
1sun-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>
1BulletinService.java文件service层,去调用dao层的方法
01package com.sk.service;
02 
03import java.util.List;
04 
05import javax.jws.WebMethod;
06import javax.jws.WebService;
07 
08import org.json.JSONArray;
09 
10import com.sk.dao.IBulletinDao;
11import com.sk.dao.impl.BulletinDaoImpl;
12import com.sk.vo.Gpw_Bulletin_Info;
13 
14<a href="http://my.oschina.net/u/142217" class="referer" target="_blank">@WebService</a>
15public 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}
1BulletinServiceDelegate.java文件service层,WebServices对外暴露数据
01package com.sk.service;
02 
03@javax.jws.WebService(targetNamespace = "http://service.sk.com/", serviceName = "BulletinServiceService", portName = "BulletinServicePort")
04public 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部署)

1Config.java文件
01package com.sk.soap;
02 
03import java.io.IOException;
04import java.util.Properties;
05 
06public 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}
1SOAPUtil.java文件
01package com.sk.soap;
02 
03import java.io.IOException;
04 
05import org.ksoap2.SoapEnvelope;
06import org.ksoap2.serialization.SoapObject;
07import org.ksoap2.serialization.SoapSerializationEnvelope;
08import org.ksoap2.transport.HttpTransportSE;
09import org.xmlpull.v1.XmlPullParserException;
10 
11import android.util.Log;
12         
13public 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}
1dbconfig.properties文件,写链接地址
1WSDL_HTTP=http\://192.168.0.91\:8080/SK_Java_Service/
最后看一下Activity中如何通过JOSN取后台传过来的值:    
1在oncreate()方法里边调用下边方法jsonAdapter()给适配器
1List<Gpw_Bulletin_Info> list = this.jsonAdapter();
2// 加载适配器
3ListAdapter adapter = new ListAdapter(ListActivity.this, list);
4 
5list_LV.setAdapter(adapter);
01private 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    }
0 0
原创粉丝点击