package com.example.phonelocation;
import org.ksoap2.serialization.SoapObject;
import android.app.Activity;
import android.os.Bundle;
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;
public class MainActivity extends Activity {
// 声明控件
private Button btnquery;
private EditText et;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnquery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (et.getText() != null) {
System.out.println(et.getText().toString());
// 获取远程Webservice返回的对象
String detail = WebServiceUtils.getLocationInfo(et.getText().toString());
if (detail != null) {
tv.setText(detail);
}
}
}
});
}
// 初始化
private void init() {
btnquery = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.info);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
package com.example.phonelocation;
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.util.Log;
public class WebServiceUtils {
// 定义webservice的命名空间,命名空间的大小写出问题也可能报错
private static final String Service_Name = "http://WebXml.com.cn/";
// 定义提供服务的URL地址
private static final String URL = "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx";
// 调用webservice服务,获取号码归属地信息
public static String getLocationInfo(String mobileCode, String userID) {
// 调用的方法名称
String methodName = "getMobileCodeInfo";
// 创建HTTPTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;// 这句不清楚什么意思
// 使用SOAP1.1协议创建Envelope对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject soapobject = new SoapObject(Service_Name, methodName);
//将需要传递的参数通过SoapObject传递过去,如果没有参数可不设置
soapobject.addProperty("mobileCode",mobileCode);
soapobject.addProperty("userID",userID);
envelope.bodyOut = soapobject;
// 设置与.net提供的webservice有较好的兼容性
envelope.dotNet = true;
// 调用webservice
try {
ht.call(Service_Name + methodName, envelope);
if (envelope.getResponse()!= null) {
//获取服务器返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
Log.i("test",result.getProperty(methodName
+ "Result").toString());
// SoapObject detail = (SoapObject) result.getProperty(methodName
// + "Result");
return result.getProperty(methodName+"Result").toString();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//没有userID的电话信息查询
public static String getLocationInfo(String mobileCode){
return getLocationInfo(mobileCode, "");
}
}
在使用android访问Web Service的时候,需要在工程中导入ksoap2-android的jar包,下载地址:ksoap2-android
工程右键-》Builder Path-》Add External Archives-》选择要导入的包。
导入包之后还需要BuilderPath-》中Config中在ksoap2-android的jar包前打上勾。
另外,还需要在Androidmanifest.xml中添加user-permission权限,在<manifest...></manifest>之间添加
<uses-permission android:name="android.permission.INTERNET"/>
即可
另外,获得WebService返回值的方法有两种:
(1)使用getResponse方法返回数据。
(2)使用bodyId及getProperty方法返回数据。