????? 本文主要采用KSOAP2-Android的方式訪問WebService。
???????? 采用WebService的地址:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
?
如下:
?
?
?
?
?
?
?
?
?
?
package com.easyway.android.ws;
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.StrictMode;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
*
* Android平臺調(diào)用WebService(手機(jī)號碼歸屬地查詢)
*
*
* 添加Ksoap2android的方式如下:
* 1.在eclipse創(chuàng)建user library
* 2.點擊項目選擇“Build Path”的“Configurtion Biuid Path ”
* 3.點擊相關(guān)的" add libray "選擇相關(guān)的jar對一個的library即可
*
*
*
* WebService的路徑如下:
* http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
*
* 采用KSOAP2Android 調(diào)用WebService服務(wù)需要采用知道WebService相關(guān)的信息如下
* 命名空間
* String nameSpace = "http://WebXml.com.cn/";
* 調(diào)用的方法名稱
* String methodName = "getMobileCodeInfo";
* EndPoint
* String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
* SOAP Action
* String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
*
*
* 聯(lián)網(wǎng)相關(guān)的服務(wù)必須(Android2.2以上的版本 )兩個條件:
* 1.添加網(wǎng)絡(luò)訪問的權(quán)限
* <uses-permission android:name="android.permission.INTERNET"/>
* 2.添加網(wǎng)絡(luò)訪問的策略
* //添加版本兼容性的網(wǎng)絡(luò)訪問限制性模式
* //設(shè)置相關(guān)的線程模式
* StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
* .detectDiskReads()
* .detectDiskWrites()
* .detectNetwork() // or .detectAll() for all detectable problems
* .penaltyLog()
* .build());
* //設(shè)置相關(guān)的虛擬機(jī)策略
* StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
* .detectLeakedSqlLiteObjects()
* //.detectLeakedClosableObjects()
* .penaltyLog()
* .penaltyDeath()
* .build());
*
*
*
* @author longgangbai
*
* @date 2011年11月29日16:26:54
*/
public class AndroidQueryTelCodeWSActivity extends Activity {
private EditText phoneSecEditText;
private TextView resultView;
private Button queryButton;
/**
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
//添加版本兼容性的網(wǎng)絡(luò)訪問限制性模式
//設(shè)置相關(guān)的線程模式
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
//設(shè)置相關(guān)的虛擬機(jī)策略
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
//.detectLeakedClosableObjects()
.penaltyLog()
.penaltyDeath()
.build());
super.onCreate(savedInstanceState);
this.setTitle(AndroidQueryTelCodeWSActivity.class.getName());
//獲取手機(jī)屏幕大小的設(shè)置
getMobileSize();
//設(shè)置相關(guān)的內(nèi)容視圖布局
setContentView(R.layout.main);
//初始化視圖的方法
initView();
//設(shè)置事件的監(jiān)聽器器方法
setEventListener();
}
/**
* 設(shè)置視圖的信息
*/
private void initView() {
//
phoneSecEditText = (EditText) findViewById(R.id.phone_sec);
resultView = (TextView) findViewById(R.id.result_text);
queryButton = (Button) findViewById(R.id.query_btn);
}
/**
* 設(shè)置事件的監(jiān)聽方法
*/
private void setEventListener() {
queryButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 手機(jī)號碼(段)
String phoneSec = phoneSecEditText.getText().toString().trim();
// 簡單判斷用戶輸入的手機(jī)號碼(段)是否合法
if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 給出錯誤提示
phoneSecEditText.setError("您輸入的手機(jī)號碼(段)有誤!");
phoneSecEditText.requestFocus();
// 將顯示查詢結(jié)果的TextView清空
resultView.setText("");
return;
}
// 查詢手機(jī)號碼(段)信息
getRemoteInfo(phoneSec);
}
});
}
/**
* 獲取手機(jī)的屏幕的大小的方法
*/
private void getMobileSize() {
//Android開發(fā)中經(jīng)常需要獲得手機(jī)屏幕的大小,
//常用的方法就是用 DisplayMetrics 類來獲取手機(jī)畫面寬高
DisplayMetrics displaysMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaysMetrics);
//設(shè)置手機(jī)的標(biāo)題
setTitle("手機(jī)屏幕分辨率:" + displaysMetrics.widthPixels + "*" +displaysMetrics.heightPixels) ;
}
/**
* 手機(jī)號段歸屬地查詢
*
* @param phoneSec 手機(jī)號段
*/
public void getRemoteInfo(String phoneSec) {
// 命名空間
String nameSpace = "http://WebXml.com.cn/";
// 調(diào)用的方法名稱
String methodName = "getMobileCodeInfo";
// EndPoint
String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
// SOAP Action
String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
// 指定WebService的命名空間和調(diào)用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 設(shè)置需調(diào)用WebService接口需要傳入的兩個參數(shù)mobileCode、userId
rpc.addProperty("mobileCode", phoneSec);
// 生成調(diào)用WebService方法的SOAP請求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut = rpc;
// 設(shè)置是否調(diào)用的是dotNet開發(fā)的WebService
envelope.dotNet = true;
// 等價于envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 調(diào)用WebService
transport.call(soapAction, envelope);
} catch (Exception e) {
e.printStackTrace();
}
// 獲取返回的數(shù)據(jù)
SoapObject object = (SoapObject) envelope.bodyIn;
String result =null;
// 獲取返回的結(jié)果
if(object==null){
result="沒有查詢到相關(guān)的區(qū)域 ";
}else{
result =object.getProperty(0).toString();
}
// 將WebService返回的結(jié)果顯示在TextView中
resultView.setText(result);
}
}
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

