Android手机归属地查询工具

在Android应用中,我们经常会与网络上的服务端的程序(J2EE或者.NET等应用)进行交互,通信。

本实例将向大家详细介绍,在android中如何调用服务器端提供的webservice,实现典型的分布式应用。

package cn.itcast.mobile.address; import java.io.InputStream; import cn.itcast.service.MobileInfoService; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private EditText mobileText; private TextView addressView; private static final String TAG = "MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mobileText = (EditText)this.findViewById(R.id.mobile); addressView = (TextView)this.findViewById(R.id.address); Button button = (Button)this.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String mobile = mobileText.getText().toString(); InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("mobilesoap.xml"); try { addressView.setText(MobileInfoService.getMobileAddress(inStream, mobile)); } catch (Exception e) { Log.e(TAG, e.toString()); Toast.makeText(MainActivity.this, "查询失败", 1).show(); } } }); } }
界面文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/mobile" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mobile" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/address" /> </LinearLayout>
重点实现代码:

package cn.itcast.service; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import cn.itcast.utils.StreamTool; public class MobileInfoService { private static String readSoapFile(InputStream inStream, String mobile) throws Exception{ byte[] data = StreamTool.readInputStream(inStream); String soapxml = new String(data); Map<String, String> params = new HashMap<String, String>(); params.put("mobile", mobile); return replace(soapxml, params); } public static String replace(String xml, Map<String, String> params)throws Exception{ String result = xml; if(params!=null && !params.isEmpty()){ for(Map.Entry<String, String> entry : params.entrySet()){ String name = "\\{1}quot;+ entry.getKey(); Pattern pattern = Pattern.compile(name); Matcher matcher = pattern.matcher(result); if(matcher.find()){ result = matcher.replaceAll(entry.getValue()); } } } return result; } public static String getMobileAddress(InputStream inStream, String mobile)throws Exception{ String soap = readSoapFile(inStream, mobile); byte[] data = soap.getBytes(); URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5 * 1000); conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据 conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); outStream.close(); if(conn.getResponseCode()==200){ return parseResponseXML(conn.getInputStream()); } return null; } private static String parseResponseXML(InputStream inStream) throws Exception{ XmlPullParser parser = Xml.newPullParser(); parser.setInput(inStream, "UTF-8"); int eventType = parser.getEventType();//产生第一个事件 while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文档结束事件 switch (eventType) { case XmlPullParser.START_TAG: String name = parser.getName();//获取解析器当前指向的元素的名称 if("getMobileCodeInfoResult".equals(name)){ return parser.nextText(); } break; } eventType = parser.next(); } return null; } }
最后提醒大家一句,记得在项目清单文件中,加入网络访问权限。

<!-- 访问网络的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值