package com.test.weather;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* 查询手机号码归属地
* @author Leo
*
*/
public class WeatherActivity extends Activity {
private EditText number = null;
private Button search = null;
private TextView result = null;
//命名空间
private static final String NAMESPACE = "http://WebXml.com.cn/";
//方法名字
private static final String METHOD_NAME = "getMobileCodeInfo";
//终点地址
private static final String ENDPOINT = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
//服务动作
private static final String SOAP_ACTION = "http://WebXml.com.cn/getMobileCodeInfo";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
number = (EditText) findViewById(R.id.number);
result = (TextView) findViewById(R.id.result);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String key = number.getText().toString().trim();
SoapObject detail = getInformation(key);
String string = detail.getProperty(0).toString();
result.setText(string);
}
});
}
private SoapObject getInformation(String key) {
//定义一个SoapObject对象并初始化命名空间和方法名字
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
//添加所需属性
rpc.addProperty("mobileCode", key);
rpc.addProperty("userID", "");
//生成SOAP请求信息
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
//创建HttpTransportSE对象进行请求
HttpTransportSE transport = new HttpTransportSE(ENDPOINT);
try {
transport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}
//得到结果并返回
return (SoapObject) envelope.bodyIn;
}
}