Search code examples
javaandroidweb-servicesconnectionksoap2

Connecting to WCF with KSOAP2 - XmlPullParserException position END_DOCUMENT


I have developed a WCF Service .NET4 and I am trying to connect to it from Android(1.6). But when i try to connect all i get is "org.xmlpull v1 xmlpullparserexception unexpected type (position: END_DOCUMENT null@1:0 in java.io.InputStreamReader@4396aa90)" error.. I've already checked METHOD_NAME , NameSpace, URL , Soap_Action and look fine for me. Also i tried with System.setProperty("http.keepAlive", "false") and changing SoapEnvelope.VER from 10 to 12 but still nothing.. Any suggestions?
My code:

package web.service;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import org.w3c.dom.Text;

 public class WebServiceConnActivity extends Activity {

private static final String NAMESPACE = "http://tempuri.org/" ;
private static final String URL = "http://10.0.2.2:51599/Service.svc";
//private static final String URL = "http://10.0.2.2:51599/MyWCFApp/Service.svc";
private static final String Add_SOAP_ACTION = "http://tempuri.org/IService/HelloWorld";
private static final String METHOD_NAME1 = "HelloWorld";



public TextView tView;
public Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tView = (TextView)findViewById(R.id.tView);
    btn = (Button)findViewById(R.id.btn);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            GetAdd();
        }
    });

}


public void GetAdd() 
{

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

    //request.addProperty("value1", "2");
    //request.addProperty("value2", "3");

    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    System.setProperty("http.keepAlive", "false");
    try
    {
        androidHttpTransport.call(Add_SOAP_ACTION, envelope);
        //java.lang.String receivedInt = (String)envelope.getResponse();
        Object result = (Object)envelope.getResponse();

        tView.setText(result.toString());

    }
    catch(Exception e)
    {
        tView.setText(e.toString());
    }

}  
}

I've spent little more time on this problem and here's what come into my mind: The problem is probably in URL - I've developed WS .NET3.5 and all works just fine, and only difference is URL:

private static final String URL = "http://10.0.2.2:50915/aspWebService/Service.asmx";

But I have no clue where i could make mistake in WCF's URL. I took URL from WDSL and i just change "localhost" to "10.0.2.2", the address http://10.0.2.2:51599/Service.svc is visible from emulators browser. Here is part of my WDSL file with URL(i suppose):

<wsdl:service name="Service">
<wsdl:port name="WSHttpBinding_IService" binding="tns:WSHttpBinding_IService">
<soap12:address location="http://localhost:51599/Service.svc"/>
<wsa10:EndpointReference>
<wsa10:Address>http://localhost:51599/Service.svc</wsa10:Address>
<Identity>
<Dns>localhost</Dns>
</Identity>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>

Any hints?


Solution

  • Problem solved. If you are using Visual Studio's Development Server to test the WCF service, you may need to deploy the service to IIS. How to do it you will find at: http://msdn.microsoft.com/en-us/library/aa751792.aspx

    Then make changes in NAMESPACE, URL

    private static final String NAMESPACE = "http://tempuri.org/" ;
    private static final String URL = "http://MyWCFApp/Service.svc";