从android模拟器调用asp.net Web服务时出错

时间:2014-05-10 13:27:51

标签: android

我无法通过我的Android代码调用我的asp.net webservice。它抛出错误并关闭应用程序说 “遗憾的是WebServiceSample已停止工作” 。我无法找到原因。

我有一个asp.net webservice,它接受2个数字作为输入并执行添加并返回结果。此Web服务正在我的本地IIS服务器中运行。

The default package is "http://tempuri.org/"  
The url of my web method is "http://192.168.0.109:15700/AddDemo.asmx"      

其中15700是IIS提供的临时端口,AddDemo是我的Web方法。

当我在浏览器中查看服务时,它运行正常,但是当我通过我的CallWebService.java调用它时,它无法正常工作。请帮我执行。是否需要进行一些额外的设置来调用从Android模拟器托管在本地系统中的asp.net Web服务?

我的CallWebService.java代码如下:     package com.example.webservicesample;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
//import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;

public class CallWebService
{
String namespace = "http://tempuri.org/";
private String url = "http://192.168.0.106:15700/AddDemo.asmx";

String res;

String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
//AndroidHttpTransport androidHttpTransport;
HttpTransportSE androidHttpTransport;

public CallWebService()
{

}

protected void setEnvelope()
{
    try
    {
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        //androidHttpTransport = new AndroidHttpTransport(url);
        //androidHttpTransport.debug = true;
        androidHttpTransport = new HttpTransportSE(url);
        androidHttpTransport.debug = true;
    }
    catch(Exception e)
    {
        res = "Error in setEnvelope....Details\n "+e.getMessage().toString();
    }
}

public String Add(String methodName,int param1, int param2)
{
    try
    {
        SOAP_ACTION = namespace + methodName;
        request = new SoapObject(namespace, methodName);

        PropertyInfo addData =new PropertyInfo();
        addData.setName("param1");
        addData.setValue(param1);
        addData.setType(Integer.class);
        request.addProperty(addData);

        addData.setName("param2");
        addData.setValue(param2);
        addData.setType(Integer.class);
        request.addProperty(addData);

        setEnvelope();

        try 
        {    
            //SOAP calling webservice
            androidHttpTransport.call(SOAP_ACTION, envelope); 
            res = envelope.getResponse().toString(); 
        } 
        catch (Exception e) 
        {
            res = "Error in Add method's 2nd catch block. Details "+e.getMessage().toString();
        }
    }
    catch(Exception e)
    {
        res = "Error in Add method's first catch block. Details "+e.getMessage().toString();
    }
    return res;
}
}

0 个答案:

没有答案