简体   繁体   中英

Call asmx Web service from android app using KSOAP

I am trying to send some data to a asmx soap web service, been trying but did managed to send. the error I got is:

08-13 20:51:12.571: W/System.err(8885): SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Value cannot be null. 08-13 20:51:12.571: W/System.err(8885): SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Value cannot be null. 08-13 20:51:12.571: W/System.err(8885): SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Value cannot be null.

Here is the web servcie URL: http://87.248.129.182:8090/PostPhotoInfo.asmx

and the code:

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();
        }
    }  

Any hint is much appreciated.

The code is not setting the NAMESPACE and not adding the photoArray

Try the following code:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM