UI设计
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="请输入你要查询的手机号" />
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询" />
<TextView
android:id="@+id/place"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
代码实现:
编写SoapUtil类访问WebService:
1.发布手机号查询WebService的地址为:http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
2.libs拷贝第三方提供的soap包:ksoap2-android-assembly-2.4-jar-with-dependencies.jar
3.编写SoapUtil类
public class SoapUtil {
public static Object doTransport(String wsdlUrl, String webMethod,
String mobileCode, String userID) {
String namespace = "http://WebXml.com.cn/";// 命名空间
SoapObject soapObject = new SoapObject(namespace, webMethod);// 创建soapobject对象
soapObject.addProperty("mobileCode", mobileCode);// 为webmethod方法设置方法参数
soapObject.addProperty("userID", null);// 为webmethod方法设置方法参数
SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 创建SoapSerializationEnvelope对象,并设置Webservice版本号
soapSerializationEnvelope.bodyOut = soapObject;
soapSerializationEnvelope.setOutputSoapObject(soapObject);
soapSerializationEnvelope.dotNet = true;
HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);
String SOAP_ACTION = "http://WebXml.com.cn/" + webMethod;
System.out.println(webMethod);
System.out.println(SOAP_ACTION);
try {
httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
if (soapSerializationEnvelope.getResponse() != null) {
Object result = soapSerializationEnvelope.getResponse();
return result;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
}
4.编写控制器MainAcytivity.java
public class MainActivity extends Activity {
private TextView place;
private EditText numberText;
private Button query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
// 查询按钮的触发事件的处理
query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = numberText.getText().toString();
String wsdlUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
String webMethod = "getMobileCodeInfo";
Object result = SoapUtil.doTransport(wsdlUrl, webMethod,
number, null);
System.out.println(result);
String numberPlace = result.toString();
place.setText(numberPlace);
}
});
}
public void init() {
// 查找控件
numberText = (EditText) this.findViewById(R.id.number);
query = (Button) this.findViewById(R.id.query);
place = (TextView) this.findViewById(R.id.place);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
注意:不要忘加权限
<uses-permission android:name="android.permission.INTERNET"/>