㈠ android webservice https 怎麼進行ssl雙向驗證
1)客戶對服舉頌務器的身份認證:
SSL伺服器允許客戶的瀏覽器使用標準的公鑰正晌鄭加密技術和一些可靠的認謹襪證中心(CA)的證書,來確認伺服器的合法性。
2)伺服器對客戶的身份認證:
也可通過公鑰技術和證書進行認證,也可通過用戶名,password來認證。
3)建立伺服器與客戶之間安全的數據通道:
SSL要求客戶與伺服器之間的所有發送的數據都被發送端加密、接收端解密,同時還檢查數據的完整性。
㈡ Android Studio如何調用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返回anyType{}
最好用念悄鬧JSON字元串,WebService返回的時候,把DataTable或者DataSet轉成Json字元串。然後安卓這邊解析運判Json字元串獲取DataTable和DataSet。因為WebService直接返仔罩回的表的xml格式,安卓解析起來比較不方便。
㈣ 怎麼解析android訪問webservice返回的SoapObject數據
SoapObject result = (SoapObject)envelope.getResponse();
result = (SoapObject)result.getProperty(1);
result = (SoapObject)result.getProperty(0);
for(int i=0; i<閉毀裂轎閉 result.getPropertyCount(); i++ ){
HashMap<String, String> map=new HashMap<String, String>();
SoapObject soap = (SoapObject) result.getProperty(i);
String xm =soap.getProperty("xm"余手).toString();
String zhuangtai =soap.getProperty("wfState").toString();
String di =soap.getProperty("wfAdd").toString();
㈤ 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;
}
㈥ 用web service方法使android連接到SQL sever的具體代碼
1.可以改用SQL Server身份驗證方式。在安全性-登錄名中添加一個SQL Server身份驗證方式登錄的用戶。
C#的代碼裡面資料庫連接字元串還是粘貼屬性裡面的連接字元串,把密碼改成自己的密碼。
private String ConServerStr = "Data Source=2013-20160523DL;Initial Catalog=test;User ID=houjingyi;Password=*******";
2.一定要先在webservice裡面確認對資料庫的操作沒有問題,謹旦再去調android程序。只看到頁面出來了很可能資料庫連接有問題,這樣即使android程序沒問題也調不出來。
3.android4.0以後不允許在主線程中訪問網路,因為萬一主線程阻塞了,會使得界面沒有響應。我們開啟線程訪問即可。
[java]view plain
publicList<HashMap<String,String>>getAllInfo(finalHandlermyhandler)
{
HashMap<String,String>tempHash=newHashMap<String,String>();
List<HashMap<String,String>>list=newArrayList<HashMap<String,String>>();
tempHash.put("Cno","Cno");
tempHash.put("Cname","Cname");
tempHash.put("Cnum","祥型擾Cnum");
list.clear();
arrayList1.clear();
arrayList2.clear();
arrayList3.clear();
list.add(tempHash);
newThread()
{
publicvoidrun()
{
arrayList1=Soap.GetWebServer("selectAllCargoInfor",arrayList1,arrayList2);
Messagemsg=newMessage();
msg.what=0x123;
msg.obj=arrayList1;
myhandler.sendMessage(msg);
}
}.start();
returnlist;
}
publicvoidinsertCargoInfo(StringCname,StringCnum)
{
arrayList1.clear();
arrayList2.clear();
arrayList1.add("Cname");
arrayList1.add("Cnum");
arrayList2.add(Cname);
arrayList2.add(Cnum);
newThread()
{
publicvoidrun()
{
try
{
Soap.GetWebServer("insertCargoInfo",arrayList1,arrayList2);
}
catch(Exceptione)租襲
{
}
}
}.start();
}
publicvoiddeleteCargoInfo(StringCno)
{
arrayList1.clear();
arrayList2.clear();
arrayList1.add("Cno");
arrayList2.add(Cno);
newThread()
{
publicvoidrun()
{
try
{
Soap.GetWebServer("deleteCargoInfo",arrayList1,arrayList2);
}
catch(Exceptione)
{
}
}
}.start();
}
[java]view plain
finalHandlermyhandler=newHandler()
{
publicvoidhandleMessage(Messagemsg)
{
if(msg.what==0x123)
{
ArrayList<String>drrayList=(ArrayList<String>)msg.obj;
for(intj=0;!drrayList.isEmpty()&&j+2<drrayList.size();j+=3)
{
HashMap<String,String>hashMap=newHashMap<String,String>();
hashMap.put("Cno",drrayList.get(j));
hashMap.put("Cname",drrayList.get(j+1));
hashMap.put("Cnum",drrayList.get(j+2));
list.add(hashMap);
}
adapter=newSimpleAdapter(
MainActivity.this,list,
R.layout.adapter_item,
newString[]{"Cno","Cname","Cnum"},
newint[]{R.id.txt_Cno,R.id.txt_Cname,R.id.txt_Cnum});
listView.setAdapter(adapter);
}
}
};
㈦ 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();
以下為簡單的實現一個天氣查看功能的例子:
publicclass WebService extends Activity {
privatestaticfinal String NAMESPACE ="http://WebXml.com.cn/";
// WebService地址
privatestatic String URL ="http://www.webxml.com.cn/
webservices/weatherwebservice.asmx";
privatestaticfinal String METHOD_NAME ="getWeatherbyCityName";
privatestatic String SOAP_ACTION ="http://WebXml.com.cn/
getWeatherbyCityName";
private String weatherToday;
private Button okButton;
private SoapObject detail;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(new Button.OnClickListener() {
publicvoid onClick(View v) {
showWeather();
}
});
}
privatevoid showWeather() {
String city ="武漢";
getWeather(city);
}
@SuppressWarnings("deprecation")
publicvoid 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();
}
}
privatevoid 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介面
需要引入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字元串
㈨ android 如何調用默認瀏覽器(webservice)打開網頁使用post的方式傳遞參數。
用HttpURLConnection對象,我們可以向網路發送請求參數. String requestUrl = "http://localhost:8080/itcast/contanctmanage.do"; Map<String, String> requestParams = new HashMap<String, String>(); requestParams.put("age", "12"); requestParams.put("name", "中國"); StringBuilder params = new StringBuilder(); for(Map.Entry<String, String> entry : requestParams.entrySet()){ params.append(entry.getKey()); params.append("="); params.append(URLEncoder.encode(entry.getValue(), "UTF-8")); params.append("&"); } if (params.length() > 0) params.deleteCharAt(params.length() - 1); byte[] data = params.toString().getBytes(); URL realUrl = new URL(requestUrl); HttpURLC...
㈩ 請問Android如何通過WebService連接MySQL資料庫
真正對資料庫數據進行操作的都還是webservice,
把android工程看成是網頁就可以了,網頁向伺服器發送請求,然後伺服器響應.
具體android鏈接伺服器用什麼語句,只要你理解了原理,光靠猜也猜到了.
不要忘記android-webservice-mysql之間的關系就可以了.思路要清晰.
URL載入要訪問的路徑,然後用HttpURLConnection接收url的openConnection,
然後就是各種流包裝來包裝去,
沒了.