android调用webservice接口 手机号所在地查询 新手总结

1.先下载ksoap2 的jar包,从百度中搜下就能找到下载地址,我用的是2.3版本的。<p>
2.新建一个android项目,.将下载的ksoap2的jar包放到android项目的libs文件夹下,并右击build path==》add  to buildpath (为防止出现找不到类的错误,网上都这样说)</p><p>
3.项目结构的主要的几个文件</p><p>     
项目webserviceClient</p><p>              
|----src--com.example.webserviceclient.MainActivity</p><p>              
|--res--layout--activity_main.xml</p><p>              
|--AndroidMainfest.xml</p><p>首先是布局代码:</p><p>     
<span style="color:#3333ff;">activity_main.xml</span></p>
<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" >

    <EditText
        android:id="@+id/editTextNum"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editTextNum"
        android:layout_marginTop="42dp"
        android:text="查询" />

    <TextView
        android:id="@+id/textViewResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnSearch"
        android:layout_centerVertical="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


 

然后MainActivity.java文件

package com.example.webserivceclient;

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

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
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;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class MainActivity extends Activity {

	private Button searchButton;
	private EditText numEditText;
	private TextView resultTextView;
	
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		searchButton = (Button)findViewById(R.id.btnSearch);
		numEditText = (EditText)findViewById(R.id.editTextNum);
		resultTextView = (TextView)findViewById(R.id.textViewResult);
		searchButton.setOnClickListener(new SearchBtnOnclickListener());
		 if (Build.VERSION.SDK_INT >= 11) {
		      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		      StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
		  }
	}

	@Override
	public 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;
	}

	class SearchBtnOnclickListener implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			String phoneNum = numEditText.getText().toString().trim();
		    if ("".equals(phoneNum) || phoneNum.length() < 7) {
		        Toast.makeText(getApplicationContext(), "输入手机号不合规范!",
		          Toast.LENGTH_LONG).show();
		        numEditText.requestFocus();
		        return;
		       }
		    resultTextView.setText(getRemoteInfo(phoneNum));
		}
		
	}
	
	 public String getRemoteInfo(String phoneSec) {
		  String nameSpace = "http://WebXml.com.cn/";// 命名空间
		  String methodName = "getMobileCodeInfo";// 调用的方法名称
		  String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";// EndPoint		  		  String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";// SOAP Action
		  SoapObject rpc = new SoapObject(nameSpace, methodName);
		  // 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
		  rpc.addProperty("mobileCode", phoneSec);
		  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("getMobileCodeInfoResult")
		    .toString();
		  
		  return result;
		 }

}


AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webserivceclient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
	<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.webserivceclient.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


 

 

           

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值