您的位置:首页 > 理论基础 > 计算机网络

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

2012-08-02 17:47 357 查看
继续熟悉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文件之前和解析后的的图片。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: