用WebService查询手机归属地

本文介绍了如何使用WebService进行手机归属地查询,通过SearchActivity.java的代码实现,配置相应的布局文件和权限设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看一下运行图:

 

下面看一下具体代码:

package com.phonesearch;

import com.phonesearch.web.SearchActivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
     private EditText phoneText;
     private Button  search;
     private TextView resultView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找组件
        phoneText=(EditText) this.findViewById(R.id.editText1);
        search=(Button) this.findViewById(R.id.button1);
        resultView=(TextView) this.findViewById(R.id.result);

        search.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				String phone=phoneText.getText().toString();
				String wsdlUrl="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
				String method="getMobileCodeInfo";
				Object result=SearchActivity.doTrantsport(wsdlUrl, method,phone);
				resultView.setText(result.toString());
			}
		});
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
}


SearchActivity.java

package com.phonesearch.web;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class SearchActivity{
   public static  Object doTrantsport(final String wsdlUrl,final String webMethod,String mobile){
	    String nameSpace = "http://WebXml.com.cn/";	
      	SoapObject soapObject=new SoapObject(nameSpace, webMethod);
      	soapObject.addProperty("mobileCode",mobile);
      	soapObject.addProperty("userID",null);
      	SoapSerializationEnvelope soapSerializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
      	soapSerializationEnvelope.bodyOut=soapObject;
      	soapSerializationEnvelope.dotNet=true;
      	soapSerializationEnvelope.setOutputSoapObject(soapObject);
      	System.out.println(wsdlUrl);
      	HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);
      	
      	String SOAP_ACTION="http://WebXml.com.cn/"+webMethod;
        System.out.println("*****"+SOAP_ACTION);
      	try {
			httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
		
			if(soapSerializationEnvelope.getResponse() != null){
				Object result = soapSerializationEnvelope.getResponse();
				System.out.println("soapSerializationEnvelope.getResponse()="+result);
				return result;
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (XmlPullParserException e1) {
			e1.printStackTrace();
		}
		return null;
   }
}


布局文件:

<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:background="@drawable/blue" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="phone"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:text="@string/search" />
    
     <TextView
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1" />
</RelativeLayout>


strings.java

<resources>

    <string name="app_name">PhoneSearch</string>
    <string name="hello_world">Please input your telephone number:</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="search">Search</string>
</resources>


接下来,我们需要设置一下权限:

 <!-- 允许访问网络 -->
    <uses-permission  android:name="android.permission.INTERNET"/>


大功告成了!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值