WebService利用KSoap2调用接口



Android端例子:
首先先设置AndroidManifest.xml赋予访问网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


主Activity内容:
package com.cn;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class WebserviceActivity extends Activity {
	//WebService中的命名空间
	private static final String NAMESPACE = "http://test";
	//WebService地址
	private static String URL = "http://192.168.1.2:8080/accessWebService/services/WebServiceTest?wsdl";
	//需要调用的方法
	private static final String METHOD_NAME = "sayHello";
	//可以为null, 或者URL+方法名 (http://192.168.1.2:8080/accessWebService/services/sayHello)
	private static String SOAP_ACTION = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			//传方法参数
			rpc.addProperty("theCityName", "上海");
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE ht = new HttpTransportSE(URL);
			//AndroidHttpTransport ht = new AndroidHttpTransport(URL);
			ht.debug = true;

			ht.call(SOAP_ACTION, envelope);
			//ht.call(null, envelope);

			//SoapObject result = (SoapObject)envelope.bodyIn;
			//detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");

			SoapObject detail =(SoapObject) envelope.bodyIn;
			Toast.makeText(this, detail.toString(), Toast.LENGTH_LONG).show();

			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}



服务端调用的Java类, 如何部署WebService请参照博客

package test;

public class WebServiceTest {
	public String sayHello(String name){   
        return "Hello,"+name;  
    }  
}
 

你可能感兴趣的:(webservice)