`

WebService for Android 目前市面上博客大写的坑手机归属地查询实验

阅读更多

                             

【tips】这节课不是讲什么是webservice,因为基础的自己去了解即可,因为自己也是初学,在网上cscd这样的技术博客描写使用WebService不在少数,所以也是极其容易上手,but!!花费了两个小时之后,一直用最简单的代码运行不成功以后,开始怀疑自己怀疑人生!但是!基于对自己忠贞的信念与盲目的自信,Maybe  blind faith on  myself,所以专门去查原因,安卓在UI主线程和子线程有时候的支持真的是很大的问题,所以想到使用网络查询webservice里面手机号码归属地是不是与主线程冲突了,主线程通常指的是MainActivity,这个是UI主线程。

===================================================================================

【cause】:

1.首先

	String nameSpace = "http://WebXml.com.cn/";
			// 调用的方法名称
			String methodName = "getMobileCodeInfo";
			// EndPoint
			String endPoint = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
			
			// SOAP Action
			String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";

 获取手机归属地网址已经更新,现在大部分博客并没有更新网址,用之前大家贴到浏览器一定试下是否可用。

2.Android4.0以上不支持直接在主线程中调用Webservice,这是最大的坑。ee死在这个坑两个小时。。。。。

ok,let's  see  how  to  do  it  这两个注意到后,其实做起来就很简单了

=================================================================================

xml布局文件:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="31dp"
        android:onClick="check"
        android:text="查看手机归属地" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/check"
        android:layout_below="@+id/check"
        android:text="" />

</RelativeLayout>

 【主程序】:

package com.example.testwebservice;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

	private static final int MSG_SUCCESS = 0;// 获取信息成功的标识
	private static final int MSG_FAILURE = 1;// 获取信息失败的标识
	public Thread myThread;
	TextView info;
	String phone;
	EditText pnumber;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		info=(TextView)findViewById(R.id.info);
		pnumber=(EditText)findViewById(R.id.phone);
		
		

	}

	private Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			// 如果成功,则显示从网络获取到的图片
			case MSG_SUCCESS:
				info.setText("手机归属地:"+"\n"+msg.obj+"");
				Toast.makeText(getApplication(),
						"success",
						Toast.LENGTH_LONG).show();
			
				break;
			// 否则提示失败
			case MSG_FAILURE:
				Toast.makeText(getApplication(),
						"失败",
						Toast.LENGTH_LONG).show();
				break;
			}
		}
	};
	
	Runnable  run=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			// 命名空间
			String nameSpace = "http://WebXml.com.cn/";
			// 调用的方法名称
			String methodName = "getMobileCodeInfo";
			// EndPoint
			String endPoint = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
			
			// SOAP Action
			String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
			// 指定WebService的命名空间和调用的方法名
			SoapObject rpc = new SoapObject(nameSpace, methodName);
			// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
			rpc.addProperty("mobileCode", phone);
			rpc.addProperty("userId", "");
			// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			// 设置是否调用的是dotNet开发的WebService
			envelope.dotNet = true;
			// 等价于envelope.bodyOut = rpc;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE transport = new HttpTransportSE(endPoint);
			try {
				// 调用WebService
				transport.call(soapAction, envelope);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 获取返回的数据
			SoapObject object = (SoapObject) envelope.bodyIn;
			// 获取返回的结果
			String result = object.getProperty(0).toString();
			// 将WebService返回的结果显示在TextView中
			System.out.println(result);
			handler.obtainMessage(MSG_SUCCESS,result).sendToTarget();
			
		}
	};
	public void check(View v){
		phone=pnumber.getText().toString();
		if(myThread==null){
			myThread=new Thread(run);
			myThread.start();
			
		}else {
			Toast.makeText(
					getApplication(),
					"已经开启线程",
					Toast.LENGTH_LONG).show();
		}
		
		
	}

	
}
	

 应当注意到大部分网址都已经更新

最后  last  but not least:

权限添加!!!一定记住

  <uses-permission android:name="android.permission.INTERNET" /> 

 目前把坑补齐,大家在做的时候养成验证url的好习惯,有时候不是我们android是后台的错哈哈哈。

【实现效果】:



 

 

  • 大小: 43.5 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics