使用KSOAP从android应用程序调用asmx Web服务
问题描述:
我想将一些数据发送到asmx soap web服务,一直在尝试但是设法发送。我得到的错误是:使用KSOAP从android应用程序调用asmx Web服务
08-13 20:51:12.571:W/System.err(8885):SoapFault - faultcode:'soap:服务器'faultstring:'服务器无法处理请求。 --->值不能为空。 08-13 20:51:12.571:W/System.err(8885):SoapFault - faultcode:'soap:服务器'faultstring:'服务器无法处理请求。 --->值不能为空。 08-13 20:51:12.571:W/System.err(8885):SoapFault - faultcode:'soap:服务器'faultstring:'服务器无法处理请求。 --->值不能为空。
这里是网络servcie网址: http://87.248.129.182:8090/PostPhotoInfo.asmx
,代码:
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://87.248.129.182:8090/PostPhotoInfo.asmx";
private final String SOAP_ACTION = "http://tempuri.org/PostPhotoInfo";
private final String METHOD_NAME = "PostPhotoInfo";
public void call_asmx() {
//Create request
// photoArray
SoapObject request = new SoapObject(URL, METHOD_NAME);
//SoapObject request2 = new SoapObject(NAMESPACE, "photoArray");
SoapObject IMG = new SoapObject(URL, "PhotoInfo");
PropertyInfo _date = new PropertyInfo();
_date.setName("Date");
_date.setValue("2016-08-12 15:45:00");
_date.setType(String.class);
PropertyInfo _Latitude = new PropertyInfo();
_Latitude.setName("Latitude");
_Latitude.setValue("12.5245123");
_Latitude.setType(double.class);
PropertyInfo _longtitude = new PropertyInfo();
_longtitude.setName("Longitude");
_longtitude.setValue("32.45345");
_longtitude.setType(double.class);
PropertyInfo _CardID = new PropertyInfo();
_CardID.setName("CardID");
_CardID.setValue(14);
_CardID.setType(int.class);
PropertyInfo _ParkingNo = new PropertyInfo();
_ParkingNo.setName("ParkingNo");
_ParkingNo.setValue(12);
_ParkingNo.setType(int.class);
PropertyInfo _Image = new PropertyInfo();
_Image.setName("Image");
_Image.setValue("<< IMAGE DATA >>>");
_Image.setType(Base64.class);
IMG.addProperty(_date);
IMG.addProperty(_Latitude);
IMG.addProperty(_longtitude);
IMG.addProperty(_CardID);
IMG.addProperty(_ParkingNo);
IMG.addProperty(_Image);
request.addSoapObject(IMG);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
fahren = response.toString();
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
任何暗示是非常赞赏。
答
的代码不设置NAMESPACE
并且不添加photoArray
尝试以下代码:
private void callWebservice() {
String NAMESPACE = "http://tempuri.org/";
String URL = "http://87.248.129.182:8090/PostPhotoInfo.asmx";
String SOAP_ACTION = "http://tempuri.org/PostPhotoInfo";
String METHOD_NAME = "PostPhotoInfo";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject photoArray = new SoapObject("", "photoArray");
SoapObject photoInfo = new SoapObject("", "PhotoInfo");
PropertyInfo date = new PropertyInfo();
date.setName("Date");
date.setValue("2016-08-12 15:45:00");
date.setType(String.class);
PropertyInfo latitude = new PropertyInfo();
latitude.setName("Latitude");
latitude.setValue("12.5245123");
latitude.setType(String.class);
PropertyInfo longtitude = new PropertyInfo();
longtitude.setName("Longitude");
longtitude.setValue("32.45345");
longtitude.setType(String.class);
PropertyInfo cardID = new PropertyInfo();
cardID.setName("CardID");
cardID.setValue("18");
cardID.setType(String.class);
PropertyInfo parkingNo = new PropertyInfo();
parkingNo.setName("ParkingNo");
parkingNo.setValue("20");
parkingNo.setType(String.class);
PropertyInfo image = new PropertyInfo();
image.setName("Image");
image.setValue("<Base64 String>");
image.setType(String.class);
photoInfo.addProperty(date);
photoInfo.addProperty(latitude);
photoInfo.addProperty(longtitude);
photoInfo.addProperty(cardID);
photoInfo.addProperty(parkingNo);
photoInfo.addProperty(image);
photoArray.addSoapObject(photoInfo);
request.addSoapObject(photoArray);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setAddAdornments(false);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
String value = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}