package com.nenglong.wsclient;
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.VER10);
envelope.bodyOut = soapObject;
// 是否调用DotNet开发的WebService
envelope.dotNet = true;//这里如果设置为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());
};
};
}