android中的网络通信(五) 通过web service编程(二) 号码归属地查询

    继续熟悉web service的使用,参考http://www.webxml.com.cn/zh_cn/index.aspx提供的中英文翻译,号码归属地,天气预报,股票查询四个web的服务,上一篇调用的是天气预报的服务,这一篇调用的号码归属地的服务接口。

    号码归属地的查询相比天气预报相对简单,只需要使用Apache Http调用号码查询的方法,传递手机号码参数,服务端返回归属地的xml文件,解析xml文件即可。就像网络通信(三)中Apache Http本地实现登陆一样,不过Web Service调用的是远程的服务器,返回的是xml的文件。

   布局文件用一个EditText来输入手机号,点击提交按钮来调用查询方法,用TextView来显示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="#ffffff">
	<RelativeLayout android:id="@+id/relative"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_marginTop="20dp">
		<TextView android:id="@+id/phonetext" android:text="手机号:"
			android:textSize="24dp" android:layout_height="wrap_content"
			android:layout_width="wrap_content" android:textColor="#000000"
			android:layout_marginLeft="5dp" />
		<EditText android:id="@+id/phone" android:layout_marginLeft="85dp"
			android:layout_height="wrap_content" android:layout_width="220dp"
			android:numeric="integer" />
	</RelativeLayout>
	<Button android:id="@+id/submit" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="submit"
		android:layout_marginTop="74dp" android:textSize="23dp" />
	<TextView android:layout_marginLeft="5dp"
		android:layout_marginTop="130dp" android:id="@+id/result"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:textColor="#000000" android:textSize="29dp" />
</RelativeLayout>
java文件也比较简单,一个简单的apache 的访问传递输入的手机号,服务端返回归属地的xml文件,解析xml文件即可。
package com.phone;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Entity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author liguiwu
 * 
 */
public class webServer extends Activity {
	/** Called when the activity is first created. */

	private EditText input;
	private TextView result;
	private Button submit;
	private String getText;
	private String message=null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViews();
		

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getText = input.getText().toString();
				message=getResult(getText);
				//解析获得的xml文件
				int start=message.indexOf(getText);
				int end=message.lastIndexOf("</string>");
				String temp=message.substring(start, end);
				result.setText(temp);
			}
		});

	}

	private void findViews() {
		input = (EditText) findViewById(R.id.phone);
		result = (TextView) findViewById(R.id.result);
		submit = (Button) findViewById(R.id.submit);
	}
    //通过apache http 来获得xml信息
	private String getResult(String number) {
		String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
		HttpPost post = new HttpPost(url);
		List<NameValuePair> paras = new ArrayList<NameValuePair>();
		paras.add(new BasicNameValuePair("mobileCode", number));
		paras.add(new BasicNameValuePair("userID", ""));
		String result = null;
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paras,
					HTTP.UTF_8);
			post.setEntity(entity);
			HttpResponse response = new DefaultHttpClient().execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity());
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;

	}
}
运行结果如下,分别是解析xml文件之前和解析后的的图片。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值