原 SOAP之WebService、JSON传值问题
发表于1年前(2012-08-17 15:11) 阅读(
1248) | 评论(
0)
0人收藏此文章, 我要收藏
赞
0
首先,Server端(Java后台代码):(web.xml、sun-jaxws.xml、BulletinService.java、BulletinServiceDelegate.java)
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" > |
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> |
12 | <servlet-name>UsersServiceService</servlet-name> |
13 | <url-pattern>/BulletinServicePort</url-pattern> |
17 | <welcome-file>index.jsp</welcome-file> |
21 | com.sun.xml.ws.transport.http.servlet.WSServletContextListener |
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" > |
1 | BulletinService.java文件service层,去调用dao层的方法 |
01 | package com.sk.service; |
03 | import java.util.List; |
05 | import javax.jws.WebMethod; |
06 | import javax.jws.WebService; |
08 | import org.json.JSONArray; |
10 | import com.sk.dao.IBulletinDao; |
11 | import com.sk.dao.impl.BulletinDaoImpl; |
12 | import com.sk.vo.Gpw_Bulletin_Info; |
14 | <a href= "http://my.oschina.net/u/142217" class = "referer" target= "_blank" > @WebService </a> |
15 | public class BulletinService { |
17 | IBulletinDao bulletinDao = new BulletinDaoImpl(); |
20 | public String find() { |
21 | List<Gpw_Bulletin_Info> list = bulletinDao.findAll(); |
23 | JSONArray jsonArray = new JSONArray(list); |
24 | return jsonArray.toString(); |
1 | BulletinServiceDelegate.java文件service层,WebServices对外暴露数据 |
01 | package com.sk.service; |
03 | @javax .jws.WebService(targetNamespace = "http://service.sk.com/" , serviceName = "BulletinServiceService" , portName = "BulletinServicePort" ) |
04 | public class BulletinServiceDelegate { |
06 | com.sk.service.BulletinService bulletinService = new com.sk.service.BulletinService(); |
08 | public String find() { |
09 | return bulletinService.find(); |
然后,Client端(Android代码):(Config.java、SOAPUtil.java、dbconfig.properties子三个文件完成Android客户端的WebServices部署)
03 | import java.io.IOException; |
04 | import java.util.Properties; |
07 | private static Properties prop = new Properties(); |
11 | prop.load(Config. class .getResourceAsStream( "dbconfig.properties" )); |
12 | } catch (IOException e) { |
19 | public static final String WSDL_HTTP = prop.getProperty( "WSDL_HTTP" ); |
03 | import java.io.IOException; |
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; |
11 | import android.util.Log; |
13 | public class SOAPUtil { |
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]); |
21 | SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); |
22 | envelope.bodyOut = request; |
23 | HttpTransportSE ht = new HttpTransportSE(Config.WSDL_HTTP+service); |
25 | ht.call( null , envelope); |
26 | if (envelope.getResponse() != null ) { |
27 | return envelope.getResponse(); |
29 | } catch (IOException e) { |
31 | } catch (XmlPullParserException e) { |
1 | dbconfig.properties文件,写链接地址 |
最后看一下Activity中如何通过JOSN取后台传过来的值:
1 | 在oncreate()方法里边调用下边方法jsonAdapter()给适配器 |
1 | List<Gpw_Bulletin_Info> list = this .jsonAdapter(); |
3 | ListAdapter adapter = new ListAdapter(ListActivity. this , list); |
5 | list_LV.setAdapter(adapter); |
01 | private List<Gpw_Bulletin_Info> jsonAdapter() { |
02 | List<Gpw_Bulletin_Info> list = new ArrayList<Gpw_Bulletin_Info>(); |
04 | Object obj = SOAPUtil.TransportData( "BulletinServicePort?wsdl" , "find" , |
06 | String data = String.valueOf(obj.toString()); |
07 | Log.i( "TAG" , "data=" + data); |
10 | JSONArray json = new JSONArray(data); |
12 | for ( int i = 0 ; i < json.length(); i++) { |
13 | Gpw_Bulletin_Info info = new Gpw_Bulletin_Info(); |
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" )); |
25 | } catch (JSONException e) { |
29 | Toast.makeText(getApplicationContext(), "暂无数据!" , Toast.LENGTH_SHORT) |
所有评论(0)