Andriod调用WebService交互(.net)

android端:

final static String NAME_SPACE = "http://tempuri.org/";

final static String SERVICE_URL = "http://192.168.0.174:8025/WebService1.asmx";
// 调用的方法
String methodName = "mysql ";
String soapAction = "http://tempuri.org/mysql";
private EditText txt1,name,job;
String on;
String wo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt1 = (EditText) findViewById(R.id.editText1);
name = (EditText) findViewById(R.id.name);
job = (EditText) findViewById(R.id.job);


}
public void onclick(View view){
on=name.getText().toString().trim();
wo= job.getText().toString().trim();
get();
}
String name_1;
private void get() {

new AsyncTask<Void, Void, Void>(){


@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub

// 使用soap1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject request = new SoapObject(NAME_SPACE, methodName);
/**
* 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
* */
request.addProperty("name", on);
request.addProperty("department", wo);
// 将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
envelope.dotNet=true;
envelope.bodyOut = request;
envelope.setOutputSoapObject(request);
new MarshalBase64().register(envelope);
// 创建httpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
try {
// 调用webService
ht.call(soapAction, envelope);
if (envelope.getResponse() != null) {
SoapObject result = (SoapObject) envelope.bodyIn;
name_1 = result.getProperty(0).toString();
System.out.println("----------------------"+name);
//txt1.setText(name_1);
} else {

}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
            //数据显示
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
txt1.setText(name_1);
}


}.execute();

}

.net服务端:

  [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod(Description = "参数返回中文方法")]
        public string myhello(string name, string department)
        {
            return "您的姓名:" + name + "," + "您的单位:" + department;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值