本篇博客的目的是供我以后用到webService时候,进行参考,在这里记录一下
本篇博客写的是,利用webService查询手机号码,参考文章是Android平台调用WebService详解,但是这篇文章是2011年的,现在网络操作必须要在子线程中进行。。
效果图是:
注释很详细,对大家都有很大的帮助~~~~
package lgx.acc.updatedemo; import java.io.IOException; import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText phoneSecEditText; private TextView resultView; private Button queryButton; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //在handler里面将其显示出来 resultView.setText(msg.obj.toString()); super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 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() < 7) { // 给出错误提示 phoneSecEditText.setError("您输入的手机号码(段)有误!"); phoneSecEditText.requestFocus(); // 将显示查询结果的TextView清空 resultView.setText(""); return; } // 查询手机号码(段)信息 getRemoteInfo(phoneSec); } }); } private void getRemoteInfo(String phoneSec) { final String namespace = "http://WebXml.com.cn/"; String methodName = "getMobileCodeInfo"; String serviceUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; final String soapAction = namespace + methodName; // 1.创建SoapObject的命名空间和调用的方法名 SoapObject soapObject = new SoapObject(namespace, methodName); // 2.配置soapObject的两个参数 soapObject.addProperty("mobileCode", phoneSec); soapObject.addProperty("userId", ""); // 3. 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号 final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER10); // 4.设置是否调用的是.Net开发的WebService envelope.setOutputSoapObject(soapObject); envelope.dotNet = true; // 5.创建HttpTransportSE对象,传递WebService服务器地址 final HttpTransportSE transportSE = new HttpTransportSE(serviceUrl); Runnable runnable = new Runnable() { @Override public void run() { try { //调用webService transportSE.call(soapAction, envelope); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } try { //获取数据 Object result = envelope.getResponse(); Message msg = Message.obtain(); msg.obj = result; handler.sendMessage(msg); } catch (SoapFault e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; new Thread(runnable).start(); } }
关于这篇博客的其他内容,你可以查看Android平台调用WebService详解,讲的很详细
关于webService的更多基础知识,你可以查看Android开发之WebService介绍
讲解完毕~~~~
时间: 2024-10-07 23:46:49