android调用.net的webservice的例子获取手机的归属地
来源:互联网 发布:陈都灵演技知乎 编辑:程序博客网 时间:2024/05/21 18:32
package com.example.usewebservice;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import android.os.Bundle;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.annotation.SuppressLint;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;import android.widget.Toast;public class MainActivity extends Activity{/** * * Android平台调用WebService(手机号码归属地查询) * @author yejianping * @date 2014-4-3 * 要先把ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar这个库导入到lib里面 * **/public EditText text ;public Button button;public TextView tx;public String telephone_number;public MyThread thread;public Handler handler; @SuppressLint("HandlerLeak")@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text = (EditText)findViewById(R.id.editText1);button = (Button)findViewById(R.id.button1);thread = new MyThread();button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){// TODO 自动生成的方法存根 telephone_number = text.getText().toString().trim();if(telephone_number.equals("")||telephone_number.length()<7){text.setError("您输入的手机号码(段)有误!");text.requestFocus();}else{new Thread(thread).start();;}}});handler = new Handler() {@SuppressLint("HandlerLeak")public void handleMessage(Message msg) {switch (msg.what) {case 0x01:Bundle bundle = new Bundle(); bundle = msg.getData(); Toast.makeText(MainActivity.this, bundle.getString("result"), Toast.LENGTH_SHORT).show();}}};}//创建线程 public class MyThread implements Runnable { public void run() { Looper.prepare();//创建本线程的消息队列并初始化 getTelephoneInfo(telephone_number); Looper.loop();//开始运行消息队列 } }public void getTelephoneInfo(String phone_number){//命名空间String nameSpace = "http://WebXml.com.cn/";//调用的方法名称String methodName = "getMobileCodeInfo";// webservice的网址String URL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";//命名空间+方法String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";// 指定WebService的命名空间和调用的方法名SoapObject rpc = new SoapObject(nameSpace, methodName);// 设置需调用WebService接口需要传入的两个参数mobileCode、userIdrpc.addProperty("mobileCode", phone_number);rpc.addProperty("userId", "");// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = rpc;// 设置是否调用的是dotNet开发的WebServiceenvelope.dotNet = true;// 等价于envelope.bodyOut = rpc;envelope.setOutputSoapObject(rpc);HttpTransportSE transport = new HttpTransportSE(URL);try {// 调用WebServicetransport.call(soapAction, envelope);}catch (Exception e){e.printStackTrace();} // 获取返回的数据SoapObject object = (SoapObject) envelope.bodyIn;// 获取返回的结果String result = object.getProperty("getMobileCodeInfoResult").toString();Message msg=new Message();Bundle bundle = new Bundle();bundle.putString("result", result);msg.setData(bundle);msg.what = 0x01;handler.handleMessage(msg);//return result;// 将WebService返回的结果显示在TextView中//tx.setText(result);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
xml的代码为;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@drawable/dd" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="24dp" android:text="手机号码归属地查询" android:textSize="30sp" android:textStyle="bold" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="23dp" android:phoneNumber="true" android:hint="请至少输入你手机号码的前7位" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="查询" /> </RelativeLayout>
源码下载
0 0
- android调用.net的webservice的例子获取手机的归属地
- 简单的 Android 调用WebService 实现号码归属地查询
- webservice获取手机归属地
- asp.net 调用远程webservice(手机归属地查询)
- 我的Android进阶之旅------>Android通过调用Webservice实现手机号码归属地查询
- android 获取手机归属地
- android获取手机归属地
- android获取手机归属地
- 调用webservice手机归属地查询
- webservice-实现手机号码归属地的查询
- Android通过webservice查询手机归属地
- 手机归属地查询的部分代码
- java学习笔记之webservice(五)--实现手机号归属地的WebService调用
- android开发如何获取电话号码的归属地信息
- Android 利用 Webservice 获取手机号码归属地
- Android手机端调用webservice来获得手机归属地号码
- Android手机端调用webservice来获得手机归属地号码
- 调用第三方webservice服务,获取电话号码归属地简单例子
- 【js学习笔记-116】-----html5之跨域消息传递
- win2003下php开发有关环境搭建问题
- C#实现网页内容正文抓取
- arquillian + HSQLDB 单元测试问题整理
- hdu 最大报销额
- android调用.net的webservice的例子获取手机的归属地
- No Hibernate Session bound to thread、No Session found f
- IOS UISlider
- const
- Roman to Integer
- Ubuntu下AllJoyn源码编译方法
- 在vi中如何删除一行,复制多行,查找字符?
- Linux设备驱动程序学习(5)-高级字符驱动程序操作[(2)阻塞型I/O和休眠]
- STM32之CAN--CAN ID过滤器分析