android的实现电话号码的实现

1.电话号码查询的实现

1)实现界面设计

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入手机号码 :" />

<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />

</LinearLayout>

2)创建网上xml

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>string</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>

3.activity的java文件

package com.example.querycode;

import com.example.service.queryc;

import android.os.Bundle;
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;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
EditText et=(EditText) findViewById(R.id.et1);
TextView tv=(TextView) findViewById(R.id.tv2);
String result;
try {
result = queryc.getResult(et.getText().toString());
tv.setText(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

4.解析xmpackage com.example.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.xmlpull.v1.XmlPullParser;

import android.util.Log;
import android.util.Xml;

public class queryc {

public static String getResult(String mobile) throws Exception{
InputStream in=queryc.class.getClassLoader().getResourceAsStream("mobile.xml");
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=in.read(buffer))!=-1){
outStream.write(buffer,0,len);
}

byte[] data=outStream.toByteArray();

String xml=new String(data);
String soap=xml.replaceAll("string", mobile);

String path="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
data=soap.getBytes();

URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(50000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length",String.valueOf((data.length)));

OutputStream out=conn.getOutputStream();
out.write(data);
out.flush();
out.close();

if(conn.getResponseCode()==200){
Log.i("TAG1",String.valueOf(conn.getResponseCode()));
InputStream resultin=conn.getInputStream();
//解析xml
XmlPullParser parser=Xml.newPullParser();
parser.setInput(resultin,"utf-8");
int event=parser.getEventType();
while(event!=XmlPullParser.END_DOCUMENT){
switch(event){
case XmlPullParser.START_TAG:
if(parser.getName().equals("getMobileCodeInfoResult")){
Log.i("TAG",parser.nextText());
return parser.nextText();
}
break;
}
event=parser.next();
}

}
return "null";
}
}
5.添加权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.querycode"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.querycode.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值