SOAP之WebService、JSON传值问题

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

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <servlet>
    <description>JAX-WS endpoint - UsersServiceService</description>
    <display-name>UsersServiceService</display-name>
    <servlet-name>UsersServiceService</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>UsersServiceService</servlet-name>
    <url-pattern>/BulletinServicePort</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>
	com.sun.xml.ws.transport.http.servlet.WSServletContextListener
    </listener-class>
  </listener>
</web-app>
sun-jaxws.xml文件,和web.xml同目录下
<?xml version = "1.0"?>
<endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
	<endpoint name="BulletinServicePort" implementation="com.sk.service.BulletinService"
		url-pattern="/BulletinServicePort">
	</endpoint>
</endpoints>
BulletinService.java文件service层,去调用dao层的方法
package com.sk.service;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;

import org.json.JSONArray;

import com.sk.dao.IBulletinDao;
import com.sk.dao.impl.BulletinDaoImpl;
import com.sk.vo.Gpw_Bulletin_Info;

@WebService 
public class BulletinService {

	IBulletinDao bulletinDao = new BulletinDaoImpl();

	@WebMethod
	public String find() {
		List<Gpw_Bulletin_Info> list = bulletinDao.findAll();
		// 调用查询方法
		JSONArray jsonArray = new JSONArray(list);
		return jsonArray.toString();
	}
	
}
BulletinServiceDelegate.java文件service层,WebServices对外暴露数据
package com.sk.service;

@javax.jws.WebService(targetNamespace = "http://service.sk.com/", serviceName = "BulletinServiceService", portName = "BulletinServicePort")
public class BulletinServiceDelegate {

	com.sk.service.BulletinService bulletinService = new com.sk.service.BulletinService();

	public String find() {
		return bulletinService.find();
	}
}

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

Config.java文件
package com.sk.soap;

import java.io.IOException;
import java.util.Properties;

public class Config {
	private static Properties prop = new Properties();
	static{
		try {
			//加载配置文件dbonfig.properties
			prop.load(Config.class.getResourceAsStream("dbconfig.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//获取dbconfig.properties中的值
	public static final String WSDL_HTTP = prop.getProperty("WSDL_HTTP");
	
}
SOAPUtil.java文件
package com.sk.soap;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
        
public class SOAPUtil {

	public static Object TransportData(final String service, final String webMethod,Object[] params){
		SoapObject request = new SoapObject("http://service.sk.com/", webMethod);		
		for(int i=0;i<params.length;i++){
			Log.v("params", params[i].toString());
			request.addProperty("arg"+i,params[i]);
		}		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);			
		envelope.bodyOut = request;			
		HttpTransportSE ht = new HttpTransportSE(Config.WSDL_HTTP+service);
		try {			
			ht.call(null, envelope);				
			if (envelope.getResponse() != null) {				
				return envelope.getResponse();
			}		
		} catch (IOException e) {
			e.printStackTrace();					
		} catch (XmlPullParserException e) {
			e.printStackTrace();					
		}		
		return null;
	}
	
}
dbconfig.properties文件,写链接地址
WSDL_HTTP=http\://192.168.0.91\:8080/SK_Java_Service/
最后看一下Activity中如何通过JOSN取后台传过来的值:    
在oncreate()方法里边调用下边方法jsonAdapter()给适配器
List<Gpw_Bulletin_Info> list = this.jsonAdapter();
// 加载适配器
ListAdapter adapter = new ListAdapter(ListActivity.this, list);

list_LV.setAdapter(adapter);
private List<Gpw_Bulletin_Info> jsonAdapter() {
		List<Gpw_Bulletin_Info> list = new ArrayList<Gpw_Bulletin_Info>();

		Object obj = SOAPUtil.TransportData("BulletinServicePort?wsdl", "find",
				new Object[] {});
		String data = String.valueOf(obj.toString());
		Log.i("TAG", "data=" + data);
		if (data != null) {
			try {
				JSONArray json = new JSONArray(data);

				for (int i = 0; i < json.length(); i++) {
					Gpw_Bulletin_Info info = new Gpw_Bulletin_Info();

					info.setBulletinid(((JSONObject) json.get(i))
							.getInt("bulletinid"));
					info.setTitle(((JSONObject) json.get(i)).getString("title"));
					info.setHtmlcontent(((JSONObject) json.get(i))
							.getString("htmlcontent"));
					info.setStartdate(((JSONObject) json.get(i))
							.getString("startdate"));

					list.add(info);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			Toast.makeText(getApplicationContext(), "暂无数据!", Toast.LENGTH_SHORT)
					.show();
		}
		return list;
	}



你可能感兴趣的:(java,android,json,SOAP,传值,WebServices)