android调用web接口,Android调用webservice 接口

以查询手机号码归属地的Web service为例,它的wsdl为

1 在Android项目中导入??Ksoap2-android jar第三方jar包??

2 Activity代码

public class SecondActivity extends Activity {

private EditText phoneSecEditText;

private TextView resultView;

private Button queryButton;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

phoneSecEditText = (EditText) findViewById(R.id.phone_sec);

resultView = (TextView) findViewById(R.id.result_text);

queryButton = (Button) findViewById(R.id.query_btn);

queryButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 手机号码(段)

String phoneSec = phoneSecEditText.getText().toString().trim();

// 简单判断用户输入的手机号码(段)是否合法

if ("".equals(phoneSec) || phoneSec.length() 

// 给出错误提示

phoneSecEditText.setError("您输入的手机号码(段)有误!");

phoneSecEditText.requestFocus();

// 将显示查询结果的TextView清空

resultView.setText("");

return;

}

// 查询手机号码(段)信息

//                getRemoteInfo(phoneSec);

new Mys().execute(phoneSec) ;

}

});

}

/**

* 手机号段归属地查询

*

* @param phoneSec 手机号段

*/

public String getRemoteInfo(String phoneSec) {

// 命名空间

String nameSpace = "http://WebXml.com.cn/";

// 调用的方法名称

String methodName = "getMobileCodeInfo";

// EndPoint

String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

// SOAP Action

String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";

// 指定WebService的命名空间和调用的方法名

SoapObject rpc = new SoapObject(nameSpace, methodName);

// 设置需调用WebService接口需要传入的两个参数mobileCode、userId

rpc.addProperty("mobileCode", phoneSec);

rpc.addProperty("userId", "");

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

envelope.bodyOut = rpc;

// 设置是否调用的是dotNet开发的WebService

envelope.dotNet = true;

// 等价于envelope.bodyOut = rpc;

envelope.setOutputSoapObject(rpc);

HttpTransportSE transport = new HttpTransportSE(endPoint);

try {

// 调用WebService

transport.call(soapAction, envelope);

} catch (Exception e) {

e.printStackTrace();

}

// 获取返回的数据

SoapObject object = (SoapObject) envelope.bodyIn;

// 获取返回的结果

String result = object.getProperty(0).toString();

// 将WebService返回的结果显示在TextView中

//        resultView.setText(result);

return result ;

}

class Mys extends AsyncTask{

@Override

protected String doInBackground(String... params) {

return getRemoteInfo(params[0]) ;

}

@Override

protected void onPostExecute(String result) {

resultView.setText(result);

super.onPostExecute(result);

}

}

}

其中,

// 命名空间

String nameSpace = "http://WebXml.com.cn/";

// 调用的方法名称

String methodName = "getMobileCodeInfo";

// EndPoint

String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";

// SOAP Action

String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";

这些值分别为

命名空间、调用的方法名称、EndPoint和SOAP Action

EndPoint通常是将WSDL地址末尾的"?wsdl"去除后剩余的部分

SOAP Action通常为命名空间 + 调用的方法名称。

如图:

1c2f05b0e12da603defabf19656e8f57.png

原文:http://my.oschina.net/zhangdengjiexuyu/blog/408415

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值