简体   繁体   中英

Call to .NET web service timed out (Android dev using KSOAP2)

I'm getting a Java Socket Exception "Operation timed out" when trying to call a .NET web service method. I've followed the many examples out there on the web to get my android to call a .net web service.

I'm running the web service using VS2010 in debug mode. The web method I'm calling is very simple -> "string GetVersion()"

I've read some posts that eclipse needs to be configured to access the internet by modifying the Proxy preferences "Preferences -> General -> Network Connections" from the Window menu item. I haven't been able to figure out exactly what I need to configure in the Proxy to get things to work if that is the problem.

I've also tried to access the .net web service root page (service.asmx) from my android with no success. I can access the asmx page with no problems using IE on the local machine. I've turned off the firewall and that didn't solve anything either. This is the first time I've tried to access a web service from a remote computer when it was running using VS2010 in debug mode.

I don't know if I have a configuration issue on the eclipse side or on the VS2010 side.

I'm also running Windows 7 Home Premium.

Any ideas?

Below is a code snippet...

private static final String SOAP_ACTION = "http://192.168.1.151/MyWebService/GetVersion";
private static final String METHOD_NAME = "GetVersion";   
private static final String NAMESPACE = "http://192.168.1.151/MyWebService"; 
private static final String URL = "http://192.168.1.151/MyService.asmx";

private void Connect()
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet=true; 
    envelope.setOutputSoapObject(request); 

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    System.out.println("DEBUG >> HttpTransport.call()");

    try
    {         
       androidHttpTransport.call(SOAP_ACTION, envelope);
    }
    catch(IOException iexc)
    {
     System.out.println("EXCEPTION >> " + iexc.toString());
    }
    catch(XmlPullParserException xexc)
    {
     System.out.println("EXCEPTION >> " + xexc.toString());
    }

    try
    {
       Object result = (Object)envelope.getResponse();

       tv.setText(result.toString());
    }
    catch (SoapFault sp)
    {
     System.out.println("EXCEPTION >> " + sp.toString());
    }
}

If you are having problem regarding calling Web Service in android then You can Use below code to call the web service and get response .Make sure that your Web Service return the response in Data Table Format ..This code help you if you using data from SQL Server database .If you you using MYSQL you need to change one thing just replace word NewDataSet from sentence obj2=(SoapObject) obj1.getProperty("NewDataSet"); by DocumentElement

private static final String NAMESPACE = "http://tempuri.org/"; 
private static final String URL ="http://localhost/Web_Service.asmx?";// you can use   IP address instead of localhost
private static final String METHOD_NAME = "Function_Name";
private static final String SOAP_ACTION = NAMESPACE+METHOD_NAME;

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
request.addProperty("parm_name",prm_value);// Parameter for Method
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);//call the eb service Method
} catch (Exception e) {
e.printStackTrace();}//Next task is to get Response and format that response
SoapObject obj,obj1,obj2,obj3;
obj= (SoapObject) envelope.getResponse();
obj1=(SoapObject) obj.getProperty("diffgram");
obj2=(SoapObject) obj1.getProperty("NewDataSet");
for(int i=0;i<obj2.getPropertyCount();i++)//the method getPropertyCount() return the number of rows
{
obj3=(SoapObject) obj2.getProperty(i);  
obj3.getProperty(0).toString();//value of column 1
obj3.getProperty(1).toString();//value of column 2
//like that you will get value from each column
}

If you have any problem regarding this you can write me..

If you run your .Net webservice in VS2010's debug mode you are most likely running a web server called Cassini. Unfortunately it is not possible to access a Cassini-hosted sites remotely:

Of course you can do some port forwarding to work around this limitation.

尝试将服务部署到IIS。

Obtain the "ksoap2-android" jar file from here .

Add this as an "Add External JAR" in your Eclipse project.

Then use AndroidHttpTransport object as shown in the code snippet below :

try 
        {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            request.addProperty("a", "15");
            request.addProperty("b", "20");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true; 
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
            returnMessage = result.toString();
        }
        catch (SoapFault sf){
            returnMessage = "FAULT:\n";
            String faultString = "Code: " + sf.faultcode + "\nString: " +
            sf.faultstring;
            Log.d(TAG , "fault : " + faultString);
            returnMessage += faultString;
        }
        catch( Exception e )
        {
            Log.d(TAG , "exception e = "+ e.getMessage());
            returnMessage = "Call Exception:" + e.toString();
        }


    return returnMessage;

Hope this helps.

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