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 [email protected]
*/
@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 [email protected]
*/
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;
}
}

你可能感兴趣的:(android,webservice)