简体   繁体   中英

Android project can't connect to WCF Web Service

I need to use .Net wcf web service. but I can't success it. I saw error and it is written:

org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@46029560)

Can you help me how I use this web service?

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://tempuri.org/" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" name="Service" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<xs:import namespace="http://schemas.datacontract.org/2004/07/"/>
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<xs:element name="GetTreatmentValues">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="tID" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">...</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/">...</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">...</xs:schema>
</wsdl:types>
<wsdl:message name="IService_GetTreatmentValues_InputMessage">
<wsdl:part name="parameters" element="tns:GetTreatmentValues"/>
</wsdl:message>
<wsdl:message name="IService_GetTreatmentValues_OutputMessage">
<wsdl:part name="parameters" element="tns:GetTreatmentValuesResponse"/>
</wsdl:message>
<wsdl:portType name="IService">
<wsdl:operation name="GetTreatmentValues">
<wsdl:input wsaw:Action="http://tempuri.org/IService/GetTreatmentValues" message="tns:IService_GetTreatmentValues_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IService/GetTreatmentValuesResponse" message="tns:IService_GetTreatmentValues_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IService" type="tns:IService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetTreatmentValues">
<soap:operation soapAction="http://tempuri.org/IService/GetTreatmentValues" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service">
<wsdl:port name="BasicHttpBinding_IService" binding="tns:BasicHttpBinding_IService">
<soap:address location="http://192.168.2.7:90/Service.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

My android

private final String NAMESPACE="http://tempuri.org/";
private final String SOAPACTION="http://tempuri.org/GetTreatmentValues";
private final String METHODNAME="GetTreatmentValues";

private final String URL="http://192.168.2.7:90/IService/Service.svc?singleWsdl";

private EditText text1;
private EditText text2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tansiyon);

     Button button1=(Button) findViewById(R.id.button1);
      text1 = (EditText) findViewById(R.id.editText1);
      text2 = (EditText) findViewById(R.id.editText2);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SoapObject request = new SoapObject(NAMESPACE,METHODNAME);
            request.addProperty("tID",1);

            SoapSerializationEnvelope sp = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            sp.dotNet = true; 
            sp.setOutputSoapObject(request); 

            HttpTransportSE aht = new HttpTransportSE(URL);
            try {

                aht.call(SOAPACTION,sp);

                text2.setText("test");
            } catch (Exception ex) {
                // TODO: handle exception
                text2.setText("error");
                ex.printStackTrace();
            }
        }
    });



}

first off, I assume you didn't forget to add suitable permissions in your app's manifest.

now you have a couple of problems, first you are trying network access in the UI thread which is not allowed by Android for API 12 and up, I expect a "network on main thread exception" to appear in your LogCat. You just have to move the ksoap2 call to an AsyncTask or so.

the other problem which I faced before is that I didn't specify a namespace in my wcf web service, so tempuri.org is put by default and it was not working, I had to change it to something else and use it and then it worked like a charm.

try to solve the first problem and run the app, you may not have a problem with tempuri.org but if it still crash then change the namespace in your service, publish it and run your app again.

In the android code don't append ?WSDL. Just write address up to Service.svc or you can copy the address from <soap:address location="..."/> tag of WSDL file. I hope it might work for you.

Refer the following link for further details:

http://techzusiastic.blogspot.in/2014/11/solved-xmlpullparse-error-problem-while.html

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