简体   繁体   English

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

[英]Android project can't connect to WCF Web Service

I need to use .Net wcf web service. 我需要使用.Net wcf Web服务。 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? 您能帮我如何使用此Web服务吗?

<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. 现在您遇到了两个问题,首先是您尝试在UI线程中尝试进行网络访问,而Android 12或更高版本不允许使用该线程,我希望LogCat中会出现“主线程异常网络”。 You just have to move the ksoap2 call to an AsyncTask or so. 您只需要将ksoap2调用移至AsyncTask左右即可。

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. 我之前面临的另一个问题是,我没有在wcf Web服务中指定名称空间,因此tempuri.org默认处于放置状态,并且无法正常工作,我必须将其更改为其他名称,然后再使用它像魅力一样运作。

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. 尝试解决第一个问题并运行该应用程序,则tempuri.org可能没有问题,但是如果它仍然崩溃,请更改服务中的名称空间,然后发布并再次运行您的应用程序。

In the android code don't append ?WSDL. 在android代码中,请勿附加?WSDL。 Just write address up to Service.svc or you can copy the address from <soap:address location="..."/> tag of WSDL file. 只需将地址写到Service.svc即可,或者您可以从WSDL文件的<soap:address location =“ ...” />标记复制地址。 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 http://techzusiastic.blogspot.in/2014/11/solved-xmlpullparse-error-problem-while.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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