2015-01-31 08:50:07|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:Java, Android|来源:唯设编程网
Android开发可以使用KSoap2可以方便地调用WebService提供的方法,下面的代码实现了通过KSopa2请求WebService查询手机号码归属地的功能:
import java.io.IOException; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.StrictMode; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * @author huanglong Android 平台调用WebService(手机号码归属地查询) */ public class MainActivity extends Activity { private TextView tv_result; private EditText et_phone; private Button btn_query; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tv_result = (TextView) findViewById(R.id.result_text); et_phone = (EditText) findViewById(R.id.et); btn_query = (Button) findViewById(R.id.btn_query); btn_query.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String phone = et_phone.getText().toString().trim(); if ("".equals(phone) || phone.length() < 7) { et_phone.setText("您输入的手机号码有误"); et_phone.requestFocus(); tv_result.setText(""); return; } getRemoteInfo(phone); } }); } /** * 查询号码段归属地的方法 * * @param phone * 手机号码段 */ public void getRemoteInfo(final String phone) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // 命名空间 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 soapObject = new SoapObject(nameSpace, methodName); // 设置需要调用WebService接口的两个参数mobileCode UserId soapObject.addProperty("mobileCode", phone); soapObject.addProperty("userId", ""); // 生成调用WebService方法调用的soap信息,并且指定Soap版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = soapObject; // 是否调用DotNet开发的WebService envelope.dotNet = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE transport = new HttpTransportSE(endPoint); try { transport.call(soapAction, envelope); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 获取返回的数据 SoapObject object = (SoapObject) envelope.bodyIn; // 获取返回的结果 String result = object.getProperty(0).toString(); Message message = handler.obtainMessage(); message.obj = result; handler.sendMessage(message); } }).start(); } private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { // 将WebService得到的结果返回给TextView tv_result.setText(msg.obj.toString()); }; }; }
Android使用KSoap2调用webService的步骤分为6个:
1. 实例化soapObject对象,指定Soap的命名空间(从相关文档中可以查看WSDL命名空间以及调用方法)
// 命名空间 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 soapObject = new SoapObject(nameSpace, methodName);
2.假设方法有参数的话,添加调用的方法的参数
// 设置需要调用WebService接口的两个参数mobileCode UserId soapObject.addProperty("mobileCode", phone); soapObject.addProperty("userId", "");
3.设置SOAP请求信息(参数部分为SOAP版本号,与自己要调用的SOAP版本必须一致,也就是你导入到工程中的jar相对应的版本)
// 生成调用WebService方法调用的soap信息,并且指定Soap版本 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = soapObject; // 是否调用DotNet开发的WebService envelope.dotNet = true; envelope.setOutputSoapObject(soapObject);
4.构建传输对象,并指定WDSL文档中的URL
HttpTransportSE transport = new HttpTransportSE(endPoint);
5.调用webService(其中参数为1:命名空间+方法名称,envelope对象);
try { transport.call(soapAction, envelope); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); }
6.将返回数据通过Handler对象进行处理:
// 获取返回的数据 SoapObject object = (SoapObject) envelope.bodyIn; // 获取返回的结果 String result = object.getProperty(0).toString(); Message message = handler.obtainMessage(); message.obj = result; handler.sendMessage(message);
下面是处理代码:
private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { // 将WebService得到的结果返回给TextView tv_result.setText(msg.obj.toString()); }; };
这样就完成了。