Android开发学习
Android开发中,经常见到通过WebService接口进行数据访问查询,介绍一下网上找到的手机号吗归属地查询实例
实例:手机号吗归属地查询
新建布局文件:activity_two.xml,使用ListView显示地区列表,代码内容:
<RelativeLayout 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: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" >
<EditText
android:id="@+id/editTextNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editTextNum"
android:layout_marginTop="42dp"
android:text="查询" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnSearch"
android:layout_centerVertical="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
实现的TwoActivity.java代码如下:
package com.zhengdecai.webserviceinfo;
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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* WebService实例:手机号码归属地查询
*
* @author 郑德才
*
*/
public class TwoActivity extends Activity {
private Button searchButton;
private EditText numEditText;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
searchButton = (Button) findViewById(R.id.btnSearch);
numEditText = (EditText) findViewById(R.id.editTextNum);
resultTextView = (TextView) findViewById(R.id.textViewResult);
searchButton.setOnClickListener(new SearchBtnOnclickListener());
}
@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;
}
class SearchBtnOnclickListener implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String phoneNum = numEditText.getText().toString().trim();
if ("".equals(phoneNum) || phoneNum.length() < 7) {
// 输入的不合规范
Toast.makeText(getApplicationContext(), "输入手机号不合规范!",
Toast.LENGTH_LONG).show();
numEditText.requestFocus();
return;
}
resultTextView.setText(getRemoteInfo(phoneNum));
}
}
private String Result;
public String getRemoteInfo(String phoneSec) {
final String phone = phoneSec;
new Thread(new Runnable() {
@Override
public void run() {
// 命名空间
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 rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
rpc.addProperty("mobileCode", phone);
rpc.addProperty("userId", "");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
// 等价于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty("getMobileCodeInfoResult")
.toString();
Result = result;
}
}).start();
return Result;
}
}
模拟器上的效果:
评论列表: