http://sunwei-07.iteye.com/blog/1139814

來源:互聯網
上載者:User

 最近學習了Android怎麼訪問WebService。把過程記錄一下

1.設定模擬器可以上網
Windows下,配置好Adroid環境變數後(如將d:\android-sdk-windows-1.0_r1\tools加入系統變數PATH),在命令列視窗輸入:

emulator

啟動Android 模擬器後, 輸入:

adb shell

進入adb shell模式:

將網路連接代理設定寫入設定資料庫,假如你的上網代理IP是10.193.xx.xx:
sqlite3
/data/data/com.android.providers.settings/databases/settings.db "INSERT
INTO system VALUES(99,'http_proxy','10.193.xx.xx:1080')"

查詢一下是否成功更改了系統設定:
sqlite3
/data/data/com.android.providers.settings/databases/settings.db "SELECT *
FROM system"

注意的幾點,設定後如果上不了網可以看下DNS是否設定正確
getprop看下NET.DNS1對不對,設成和PC一樣就OK了
設定方法 setprop NET.DNS1 xxx.xxx.xxx.xxx 具體值看你自己PC

2.發布WebSerice(我是通過axis2在TOMCAT加了一個簡單的數字加減的服務)

1.下載axis2的war檔案,扔到Tomcat Webapps
2.寫Service類
package rong.service;

/** */
/**
* 計算機運算
*
* @author rongxinhua
*
*/
public class CalculateService {

/** */
/**
* 加法運算
*
* @param x
*            被加數
* @param y
*            加數
* @return x與y的和
*/
public float plus(float x, float y) {
return x + y;
}

/** */
/**
* 減法運算
*
* @param x
*            被減數
* @param y
*            減數
* @return x與y之差
*/
public float minus(float x, float y) {
return x - y;
}

/** */
/**
* 乘法運算
*
* @param x
*            被乘數
* @param y
*            乘數
* @return x與y的乘積
*/
public float multiply(float x, float y) {
return x * y;
}

/** */
/**
* 除法運算
*
* @param x
*            被除數
* @param y
*            除數
* @return x與y的商
*/
public float divide(float x, float y) {
return x / y;
}

}

3.打包服務arr檔案,扔到axis2的WEB-INF\service\下

比如:(E:\J2EE\tomcat6\webapps\axis2\WEB-INF\services\CalculateService.aar)

4.Android方面,寫個介面,簡單的需要一個TextView就需要顯示WebService返回的值

package com.suncsdn;

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;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Hello extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView t = (TextView) findViewById(R.id.text1);

String msg = getServiceStr();

t.setText(msg==null?"無值返回":msg);
}

private String getServiceStr() {

// WebService的命名空間
String namespace = "http://service.rong";
// 伺服器發布的地址
String url = "http://192.168.1.121:8080/axis2/services/CalculateService";
// 函數名
String methodName = "plus";
//傳回值
String xmlMessage = null;

try {
// 建立HttpTransportSE對象,通過HttpTransportSE類的構造方法可以指定WebService的url
HttpTransportSE transport = new HttpTransportSE(url);

transport.debug = true;

// 指定WebService的命名空間和函數名
SoapObject soapObject = new SoapObject(namespace, methodName);

// 設定調用方法參數的值
soapObject.addProperty("x", 1);
soapObject.addProperty("y", 1);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);

// 使用call方法調用WebService方法
transport.call(null, envelope);

SoapObject sb = (SoapObject)envelope.bodyIn;
// 擷取從伺服器端返回的XML字串
xmlMessage = sb.toString();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return xmlMessage;
}
}

為了方便直接把AAR檔案作為附加帶上了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.