當前位置:首頁 » 安卓系統 » android與webservice

android與webservice

發布時間: 2022-05-24 08:05:25

㈠ android端怎麼調用webservice介面

在Android平台調用Web Service需要依賴於第三方類庫ksoap2,它是一個SOAP Web service客戶端開發包,主要用於資源受限制的java環境如Applets或J2ME應用程序(CLDC/ CDC/MIDP)。認真讀完對ksoap2的介紹你會發現並沒有提及它應用於Android平台開發,沒錯,在Android平台中我們並不會直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一個高效、輕量級的SOAP開發包,等同於Android平台上的KSoap2的移植版本。
Ksoap2-android jar包下載

㈡ android怎麼調用webservice介面

使用Ksoup.jar包可以實現webservice的調用
參考代碼:
String result = null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.bodyOut = soapObject;
String endPoint = 地址後綴//如WebService/AppService.asmx
HttpTransportSE transportSE = new HttpTransportSE(endPoint);
SoapObject object = null;
transportSE.call(地址 + soapObject.getName(),
envelope);
object = (SoapObject) envelope.bodyIn;
result = object.getProperty(0).toString();

附上ksoup包

㈢ android怎麼調用webservice寫的伺服器端

轉貼:
Android調用WebService
作者:歐陽旻
WebService是一種基於SOAP協議的遠程調用標准,通過webservice可以將不同操作系統平台、不同語言、不同技術整合到一塊。在Android SDK中並沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發包對於Android系統過於龐大,也未必很容易移植到Android系統中。因此,這些開發包並不是在我們的考慮范圍內。適合手機的WebService客戶端的SDK有一些,比較常用的有Ksoap2,可以從http://code.google.com/p/ksoap2-android/downloads/list進行下載;將下載的ksoap2-android-assembly-2.4-jar-with-dependencies.jar包復制到Eclipse工程的lib目錄中,當然也可以放在其他的目錄里。同時在Eclipse工程中引用這個jar包。

具體調用調用webservice的方法為:

(1) 指定webservice的命名空間和調用的方法名,如:

SoapObject request =new SoapObject(http://service,」getName」);

SoapObject類的第一個參數表示WebService的命名空間,可以從WSDL文檔中找到WebService的命名空間。第二個參數表示要調用的WebService方法名。

(2) 設置調用方法的參數值,如果沒有參數,可以省略,設置方法的參數值的代碼如下:

Request.addProperty(「param1」,」value」);
Request.addProperty(「param2」,」value」);

要注意的是,addProperty方法的第1個參數雖然表示調用方法的參數名,但該參數值並不一定與服務端的WebService類中的方法參數名一致,只要設置參數的順序一致即可。

(3) 生成調用Webservice方法的SOAP請求信息。該信息由SoapSerializationEnvelope對象描述,代碼為:

SoapSerializationEnvelope envelope=new
SoapSerializationEnvelope(SoapEnvelope.VER11);
Envelope.bodyOut = request;

創建SoapSerializationEnvelope對象時需要通過SoapSerializationEnvelope類的構造方法設置SOAP協議的版本號。該版本號需要根據服務端WebService的版本號設置。在創建SoapSerializationEnvelope對象後,不要忘了設置SOAPSoapSerializationEnvelope類的bodyOut屬性,該屬性的值就是在第一步創建的SoapObject對象。

(4) 創建HttpTransportsSE對象。通過HttpTransportsSE類的構造方法可以指定WebService的WSDL文檔的URL:

HttpTransportSE ht=new HttpTransportSE(「http://192.168.18.17:80
/axis2/service/SearchNewsService?wsdl」);

(5)使用call方法調用WebService方法,代碼:

ht.call(null,envelope);

Call方法的第一個參數一般為null,第2個參數就是在第3步創建的SoapSerializationEnvelope對象。

(6)使用getResponse方法獲得WebService方法的返回結果,代碼:

SoapObject soapObject =( SoapObject) envelope.getResponse();

以下為簡單的實現一個天氣查看功能的例子:

public class WebService extends Activity {
private static final String NAMESPACE = "http://WebXml.com.cn/";
// WebService地址
private static String URL = "http://www.webxml.com.cn/
webservices/weatherwebservice.asmx";
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String SOAP_ACTION = "http://WebXml.com.cn/
getWeatherbyCityName";

private String weatherToday;

private Button okButton;
private SoapObject detail;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);

okButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
showWeather();
}
});
}

private void showWeather() {
String city = "武漢";
getWeather(city);
}

@SuppressWarnings("deprecation")
public void getWeather(String cityName) {
try {
System.out.println("rpc------");
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("rpc" + rpc);
System.out.println("cityName is " + cityName);
rpc.addProperty("theCityName", cityName);

AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);

ht.call(SOAP_ACTION, envelope);

SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject) result
.getProperty("getWeatherbyCityNameResult");

System.out.println("result" + result);
System.out.println("detail" + detail);
Toast.makeText(WebService.this, detail.toString(),
Toast.LENGTH_LONG).show();
parseWeather(detail);

return;
} catch (Exception e) {
e.printStackTrace();
}
}

private void parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday = "今天:" + date.split(" ")[0];
weatherToday = weatherToday + "\n天氣:" + date.split(" ")[1];
weatherToday = weatherToday + "\n氣溫:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday + "\n風力:"
+ detail.getProperty(7).toString() + "\n";
System.out.println("weatherToday is " + weatherToday);
Toast.makeText(WebService.this, weatherToday,
Toast.LENGTH_LONG).show();

}
}

㈣ android平台調用webservice介面

在Android平台調用Web Service需要依賴於第三方類庫ksoap2,它是一個SOAP Web service客戶端開發包,主要用於資源受限制的Java環境如Applets或J2ME應用程序(CLDC/ CDC/MIDP)。認真讀完對ksoap2的介紹你會發現並沒有提及它應用於Android平台開發,沒錯,在Android平台中我們並不會直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一個高效、輕量級的SOAP開發包,等同於Android平台上的KSoap2的移植版本。

㈤ android調用webservice怎麼傳遞對象

1.webservice方法要傳遞參數的對象中包含了日期類型,guid類型。如下所示:

[html] view plain
POST /MyWebService.asmx HTTP/1.1
Host: 192.168.11.62
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AddMaintenanceInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddMaintenanceInfo xmlns="http://tempuri.org/">
<model>
<Id>guid</Id>
<CarId>guid</CarId>
<Cost>string</Cost>
<Dates>dateTime</Dates>
</model>
</AddMaintenanceInfo>
</soap:Body>
</soap:Envelope>

2.新建一個類CarMaintenanceInfo用於傳遞參數對象,並使其實現KvmSerializable,如下

[java] view plain
public class CarMaintenanceInfo implements KvmSerializable {

/**
* 車輛ID
*/
public String CarId;

/**
* 車輛維修費用
*/
public String Cost;

public String Dates;

@Override
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return CarId;
case 1:
return Cost;
case 2:
return Dates;
default:
break;
}
return null;
}

@Override
public int getPropertyCount() {
return 3;
}

@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch (arg0) {
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "CarId";
break;
case 1:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Cost";
break;
case 2:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "Dates";
break;
default:
break;
}
}

@Override
public void setProperty(int arg0, Object arg1) {
switch (arg0) {
case 0:
CarId = arg1.toString();
break;
case 1:
Cost = arg1.toString();
break;
case 2:
Dates = arg1.toString();
break;
default:
break;
}

}

}

注意:getPropertyCount的值一定要與該類對象的屬性數相同,否則在傳遞到伺服器時,伺服器收不到部分對象的屬性。

3.編寫請求方法,如下:

[java] view plain
public boolean addMaintenanceInfo(Context context) throws IOException, XmlPullParserException {

String nameSpace = "http://tempuri.org/";
String methodName = "AddMaintenanceInfo";
String soapAction = "http://tempuri.org/AddMaintenanceInfo";

String url = "http://192.168.11.62:6900/MyWebService.asmx?wsdl";// 後面加不加那個?wsdl參數影響都不大

CarMaintenanceInfo info = new CarMaintenanceInfo();
info.setProperty(0, "9fee02c9-8785-4b49-b389-58ed6562c66d");
info.setProperty(1, "12778787");
info.setProperty(2, "2013-07-29T16:45:20");

// 建立webservice連接對象
org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;// 是否是調試模式

// 設置連接參數
SoapObject soapObject = new SoapObject(nameSpace, methodName);
PropertyInfo objekt = new PropertyInfo();
objekt.setName("model");
objekt.setValue(info);
objekt.setType(info.getClass());
soapObject.addProperty(objekt);

// 設置返回參數
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// soap協議版本必須用SoapEnvelope.VER11(Soap
// V1.1)
envelope.dotNet = true;// 注意:這個屬性是對dotnetwebservice協議的支持,如果dotnet的webservice
// 不指定rpc方式則用true否則要用false
envelope.bodyOut = transport;
envelope.setOutputSoapObject(soapObject);// 設置請求參數
// new MarshalDate().register(envelope);
envelope.addMapping(nameSpace, "CarMaintenanceInfo", info.getClass());// 傳對象時必須,參數namespace是webservice中指定的,

// claszz是自定義類的類型
try {
transport.call(soapAction, envelope);
// SoapObject sb = (SoapObject)envelope.bodyIn;//伺服器返回的對象存在envelope的bodyIn中
Object obj = envelope.getResponse();// 直接將返回值強制轉換為已知對象
Log.d("WebService", "返回結果:" + obj.toString());

}
catch (IOException e) {
e.printStackTrace();
}
catch (XmlPullParserException e) {
e.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}

return true;

// 解析返回的結果
// return Boolean.parseBoolean(new AnalyzeUtil().analyze(response));
}

注意:傳遞date類型的時候其實可以使用Date而不是String。但是需要修改幾個地方
1.CarMaintenanceInfo類中的getPropertyInfo(),將arg2.type = PropertyInfo.STRING_CLASS修改為MarshalDate.DATE_CLASS;
2. 在請求方法中的 envelope.setOutputSoapObject(soapObject);下加上 new MarshalDate().register(envelope);
雖然可以使用Date,但是傳到伺服器上的時間與本地時間有時差問題。

當Android 調用webservice,請求參數中有日期,guid,double時,將這些類型在添加對象前轉換為字元串即可。

[java] view plain
// 構造request
SoapObject request = new SoapObject(PublishInfo.NAMESPACE, "GetCarListByRegion");
request.addProperty("leftTopLat", String.valueOf(leftTopLat));
request.addProperty("leftTopLng", String.valueOf(leftTopLng));
request.addProperty("rightBottomLat", String.valueOf(rightBottomLat));
request.addProperty("rightBottomLng", String.valueOf(rightBottomLng));

當需要傳遞一個伺服器對象參數時.
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetLicenseDetails xmlns="http://tempuri.org/">
<companyId>guid</companyId>
<licenseNos>
<string>string</string>
<string>string</string>
</licenseNos>
<pageOptions>
<page>int</page>
<rows>int</rows>
<total>int</total>
<sort>string</sort>
<order>string</order>
<skip>int</skip>
<Remark>string</Remark>
</pageOptions>
</GetLicenseDetails>
</soap:Body>
</soap:Envelope>

[java] view plain
public ListPageResult<LicenseInfo> getLicenseDetails(String[] licenseNos, int page, int rows, int total) {
// 構造request
SoapObject request = new SoapObject(PublishInfo.NAMESPACE, "GetLicenseDetails");
// 許可證列表
SoapObject deviceObject = new SoapObject(PublishInfo.NAMESPACE, "GetLicenseDetails");
if (licenseNos != null && licenseNos.length > 0) {
for (int i = 0; i < licenseNos.length; i++) {
if (!"".equals(licenseNos[i])) {
deviceObject.addProperty("string", licenseNos[i]);
}
}
request.addProperty("licenseNos", deviceObject);
}
else {
request.addProperty("licenseNos", null);
}
// 分頁數據
SoapObject optionObject = new SoapObject(PublishInfo.NAMESPACE, "PageOptions");
optionObject.addProperty("page", page);
optionObject.addProperty("rows", rows);
optionObject.addProperty("total", total);
optionObject.addProperty("sort", null);
optionObject.addProperty("order", "desc");
optionObject.addProperty("skip", 0);
optionObject.addProperty("Remark", null);
request.addProperty("pageOptions", optionObject);
// 獲取response
Object response = sendRequest(context, request);
if (!mNetErrorHanlder.hasError(response)) { return ObjectAnalyze.getLicenseDetails(response); }
return null;
}

㈥ android怎麼連接webservice伺服器

1)新建Android工程,引入上面下載的ksoap2-android類庫
Android工程的創建就不多說了,主要想說明的是如何向Android工程中添加第三方jar包。當然,添加第3方jar的方式有多種,我個人比較喜歡用下面這種方式,即先將第三方jar包拷貝到工程某個目錄下,再將其加入到工程的Build
Path中。

㈦ android 調用WebService 出現問題

首先編譯過是最基礎的,不代表webservice沒問題。其次你沒有貼出你的錯誤信息別人也不能從旁指點。至於給個例子嗎,網上有本書叫google
android
sdk開發範例大全。有電子版的。應該可以解決你的問題了。

㈧ android怎樣調用webService

使用Ksoup.jar包可以實現webservice的調用

參考代碼:

String result = null;

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);

envelope.dotNet = true;

envelope.bodyOut = soapObject;

String endPoint = 地址後綴//如WebService/AppService.asmx

HttpTransportSE transportSE = new HttpTransportSE(endPoint);

SoapObject object = null;

transportSE.call(地址 + soapObject.getName(),

envelope);

object = (SoapObject) envelope.bodyIn;

result = object.getProperty(0).toString();


附上ksoup包


㈨ android和java webservice RSA處理的不同

android和java webservice RSA處理的不同如下:
1.最近做RSA加密用於增強android客戶機與伺服器(JavaEE)數據傳輸的安全性。發現在andorid機器上生成的(密鑰對由伺服器在windows xp下生成並將公鑰發給客戶端保存)密碼無法在伺服器通過私鑰解密。
2.為了測試,在伺服器本地加解密正常,另外,在android上加解密也正常,但是在伺服器中加密(使用相同公鑰)後的密碼同樣無法在android系統解密(使用相同私鑰)。
3.由於對RSA加密演算法不了解,而且對Java RSA的加密過程也不清楚、谷歌一番,才了解到可能是加密過程中的填充字元長度不同,這跟加解密時指定的RSA演算法有關系。
4.比如,在A機中使用標准RSA通過公鑰加密,然後在B系統中使用「RSA/ECB/NoPadding」使用私鑰解密,結果可以解密,但是你會發現解密後的原文前面帶有很多特殊字元,這就是在加密前填充的空字元;如果在B系統中仍然使用標準的RSA演算法解密,這在相同類型的JDK虛擬機環境下當然是完全一樣的,關鍵是android系統使用的虛擬機(dalvik)跟SUN標准JDK是有所區別的,其中他們默認的RSA實現就不同。
5.更形象一點,在加密的時候加密的原文「abc」,你直接使用「abc」.getBytes()方法獲得的bytes長度可能只有3,但是系統卻先把它放到一個512位的byte數組里,new byte[512],再進行加密。但是解密的時候你使用的是「加密後的密碼」.getBytes()來解密,解密後的原文自然就是512長度的數據,即是在「abc」之外另外填充了500多位元組的其他空字元。

㈩ android怎麼訪問webservice介面

需要引入ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
//WebService的命名空間
staticfinal
String
namespace
="impl.service.suncreate.com";前面加上http
//伺服器發布的url
staticfinal
String
url
=
10.100.3.41/axis2/services/UploadService;前面加上http
final
String
methodName
="upload";
//
函數名
finalint
sessionID
="111111";
//sessionID
//創建HttpTransportSE對象,通過HttpTransportSE類的構造方法可以指定WebService的url
HttpTransportSE
transport
=new
HttpTransportSE(url);
transport.debug
=true;
//指定WebService的命名空間和函數名
SoapObject
soapObject
=new
SoapObject(namespace,
methodName);
//設置調用方法參數的值
soapObject.addProperty("sessionID",
sessionID);
//sessionID
soapObject.addProperty("data",
cds);
//cds是需要傳遞的對象
SoapSerializationEnvelope
envelope
=new
SoapSerializationEnvelope(SoapEnvelope.VER10);
envelope.bodyOut
=
transport;
envelope.setOutputSoapObject(soapObject);
//使用call方法調用WebService方法
transport.call(null,
envelope);
SoapObject
sb
=
(SoapObject)
envelope.bodyIn;
String
xmlMessage
=
sb.toString();
//
獲取從伺服器端返回的XML字元串

熱點內容
安卓和csharp哪個發展好 發布:2024-05-18 18:09:30 瀏覽:526
換編程題庫 發布:2024-05-18 18:00:58 瀏覽:562
如何使用伺服器ip直連網站 發布:2024-05-18 18:00:49 瀏覽:432
三星n7100哪個安卓版本好用 發布:2024-05-18 17:55:41 瀏覽:489
萬國覺醒採集腳本源碼 發布:2024-05-18 17:55:39 瀏覽:947
sqlserver加欄位 發布:2024-05-18 17:54:53 瀏覽:928
安卓手機如何清除應用記錄 發布:2024-05-18 17:31:37 瀏覽:639
查看存儲過程許可權 發布:2024-05-18 17:18:33 瀏覽:192
php類self 發布:2024-05-18 17:15:03 瀏覽:895
手機2b2t的伺服器地址是多少 發布:2024-05-18 17:14:56 瀏覽:189