Android项目无法连接到WCF Web服务

时间:2014-02-05 13:33:03

标签: c# java android wcf

我需要使用.Net wcf网络服务。但我不能成功。我看到了错误,并写道:

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

您能帮助我使用此网络服务吗?

<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>

我的机器人

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



}

2 个答案:

答案 0 :(得分:0)

首先,我假设您没有忘记在应用的清单中添加适当的权限。

现在你有几个问题,首先你是在UI线程中尝试网络访问,这是Android for API 12不允许的,我希望你的LogCat中出现“主线程异常网络”。您只需将ksoap2调用移动到AsyncTask左右。

我之前遇到的另一个问题是我没有在我的wcf web服务中指定命名空间,所以tempuri.org默认放置并且它不起作用,我不得不将其更改为其他内容并使用它然后它就像一个魅力。

尝试解决第一个问题并运行应用程序,你可能没有tempuri.org的问题,但如果它仍然崩溃,那么更改服务中的命名空间,发布它并再次运行你的应用程序。

答案 1 :(得分:0)

在android代码中不要附加?WSDL。只需将地址写到Service.svc,或者您可以从&lt; soap:address location =&#34; ...&#34; /&gt;复制地址。 WSDL文件的标记。我希望它可能对你有用。

有关详细信息,请参阅以下链接:

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