android 调用webservice



web服务端
package cn.yue.lsp.phone.service;


import javax.jws.WebParam;
import javax.jws.WebService;


/**
* 自定义service接口
*
* @time 9:01:54 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@WebService
public interface MyService {
public String seyHello(@WebParam(name = "name")
String name);
}




package cn.yue.lsp.phone.service.impl;


import cn.yue.lsp.phone.service.MyService;


/**
* 自定义接口实现
*
* @time 9:01:46 PM
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class MyServiceImpl implements MyService {


public String seyHello(String name) {
System.out.println("进入服务器端的sayHello方法");
return "你好" + name;
}
}




其他代码略








android客户端


导入ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar包.
添加访问网络权限


public class MainActivity extends Activity {
private static final String SERVICE_NAMESPACE =


"http://service.phone.lsp.yue.cn/";
private static final String SERVICE_URL =


"http://192.168.1.5:8089/SurveyPark/services/myService";
private String methodName = "seyHello";


@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("tag", "MainActivity is starting!");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test();
}


/**
* 调用webService方法
*/
private void test() {
Log.i("tag", "test is starting!");
// 创建HttpTransportSE对象
HttpTransportSE htse = new HttpTransportSE(SERVICE_URL);
// 使用soap1.1协议,创建SoapSerializationEnvelope对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope


(SoapEnvelope.VER11);
// 创建soapObject对象,参数为命名空间和webService 方法
SoapObject request = new SoapObject(SERVICE_NAMESPACE, methodName);
// 添加参数
request.addProperty("name", "岳振华");


// 设置soap消息体
// envelope.setOutputSoapObject(request);
envelope.bodyOut = request;


try {
// 调用对象的call方法,
htse.call(null, envelope);
if (envelope.getResponse() != null) {
Log.i("tag", "返回正常");
// 取得返回的soapObject对象
SoapObject result = (SoapObject) envelope.bodyIn;
String str = result.getProperty(0).toString();
Log.i("tag", "返回值为:" + str);
} else {
Log.i("tag", "无返回值");
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值