-->

SoapObject Result returns anyType{} as value

2019-05-26 06:56发布

问题:

package com.example.gramaniladharidetails;

import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

  public class GramaNiladhari extends Activity {
private static String SOAP_ACTION1= "http://localhost/getGramaNiladhariData";
private static String NAMESPACE = "http://119.235.4.217/";
private static String METHOD_NAME1 = "getGramaNiladhariData";
private static String URL = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grama_niladhari);


    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            final String gndLifeCode = prov;
            String LifeCode = "3-2-09-060";
            request.addProperty("gndLifeCode", LifeCode);
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);


            SupportThread st = new SupportThread();
            Log.i("Request", request.toString());
            st.execute(request);

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_grama_niladhari, menu);
    return true;
}

private class SupportThread extends
        AsyncTask<SoapObject, Object, SoapObject> {

    @Override
    protected SoapObject doInBackground(SoapObject... req) {
        SoapObject result = null;
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.setOutputSoapObject(req[0]);
        envelope.addMapping(NAMESPACE, "SerializeInfo",
                new SerializeInfo().getClass());

        Marshal floatMarshal = new MarshalFloat();
        floatMarshal.register(envelope);

        envelope.dotNet = true;

        try {
            envelope.encodingStyle = SoapSerializationEnvelope.XSD;
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.debug = true;

            androidHttpTransport.call(SOAP_ACTION1, envelope);
            SoapObject result = (SoapObject) envelope.getResponse();



            if (envelope.bodyIn instanceof SoapFault) {
                String str = ((SoapFault) envelope.bodyIn).faultstring;
                Log.i("*******", str);

            } else {
                int elementCount = result.getPropertyCount();

                if (elementCount > 0) {
                    String element;

                    for (int i = 0; i < elementCount; i++) {


                        element = result.getProperty(i).toString();
                        Log.d("This is an element", Integer.toString(i)
                                + " --" + element);


                    }
                }

            }

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

        return result;
    }

    @Override
    protected void onPostExecute(SoapObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if (result != null) {
            String response_name = result.getName();

            TextView tv = (TextView) findViewById(R.id.textView5);
            tv.setText(result.getProperty(0).toString());
        }

        else {

            Toast.makeText(getApplicationContext(), "No Response",
                    Toast.LENGTH_LONG).show();

        } 
    }
}

}

The webservice method i am calling retuns a complexType Object called GramaNiladhari. It consists of 5 string types. When I run this code I get the following SoapObject result.

anyType{gnName=anyType{}; address=anyType{}; workingDays=anyType{}; gnDivision=anyType{}; contactNumber=anyType{}; } 

But here i am missing the values,and they are replaced with anyType{} . How can I retrieve the real values ?

I've edit my question.

This is the parsed request in XML.

    <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:d="http://www.w3.org/2001/XMLSchema" 
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body>
<getGramaNiladhariData xmlns="http://119.235.4.217/GNService/">
<gndLifeCode i:type="d:string">1-1-21-065</gndLifeCode>
</getGramaNiladhariData></v:Body></v:Envelope>

And this is the result after calling the web service.

 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <getGramaNiladhariDataResponse xmlns="http://localhost/"> <getGramaNiladhariDataResult><gnName /><address /><workingDays /><gnDivision /><contactNumber /></getGramaNiladhariDataResult></getGramaNiladhariDataResponse></soap:Body></soap:Envelope>

Checking the request,it seems that the request has passed well(even the request parameter gndLifeCode with its correct value, 1-1-21-065 ).

But the response, is null.All the elements I need to retrieve have empty tags.

Can this be a problem in web service. I've checked this web service in SoapUI & it works well there(even it works in SoapClient). Yet can't figure out what the issue here is?

回答1:

Sorry to say i am not a Android developer so i coudn't provide you the exact answer but i can give you some hints what going wrong with my view point

Hopefully your Request is not that much cool to understand i guess by the server

What i think is your request is seems like this

REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/">
        <SOAP-ENV:Body>
            <ns1:getGramaNiladhariData/>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

so its returns this

stdClass Object
(
    [getGramaNiladhariDataResult] => stdClass Object
        (
            [gnName] => 
            [address] => 
            [workingDays] => 
            [gnDivision] => 
            [contactNumber] => 
        )

)

So Your Request must be like this

REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost/">
        <SOAP-ENV:Body>
            <ns1:getGramaNiladhariData>
                <ns1:gndLifeCode>3-2-09-060</ns1:gndLifeCode>
            </ns1:getGramaNiladhariData>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

and then you will get the exact answer

And also make sure your tags of xmlns such as ns1, SOAP-ENV (in my case) these are same, to review that i recommend to use soap ui

Cool



回答2:

the Imports:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

Web service:

/*web service parameters*/
private static final String TAG = SearchResultFragment.class.getSimpleName();
private static final String SOAP_ACTION = "http://tempuri.org/GetMethod";
private static final String METHOD_NAME = "GetMethod";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://127.0.0.1/App_Services/service.asmx";

This is how i use it...

private SoapObject CallWebServiceDemo() {
    SoapObject result = null;
    try {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.implicitTypes = true;
        envelope.dotNet = true;
        envelope.encodingStyle = SoapSerializationEnvelope.XSD;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        result = (SoapObject) envelope.getResponse();
    } catch (Exception ex) {
        Log.e(TAG, ex.getMessage());
    }
    return result;
}


回答3:

Use this to change output to xml format:

httpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
httpTransport.call(SOAP_ACTION, soapEnvelope);
String resultString = httpTransport.responseDump;