无法从Android连接到Web服务

时间:2013-03-29 23:10:25

标签: android asp.net web-services ip

我是AndroidWebServices的新手,目前我正在阅读有关Web服务,SOAP等的许多信息。

我正在尝试编写一个Web服务示例,它只是说Hello,它可以在我的浏览器中运行但是从我的Android设备/模拟器不起作用。我已将其IP设置为我的PC ipv4地址,我已关闭防火墙和防病毒软件。但是,如果在浏览器中我尝试连接到此IP,则说404.(我不知道这是否正常)我还使用具有以下URL的本地IIS:http://localhost/HelloAndroid

以下是我的Web服务的代码:

namespace HelloAndroid
{
    [WebService(Namespace = "http://sample.com/")]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public string SayHello()
        {
            return "Hello, Android from .NET";
        }
    }
} 

Android活动:

public class SoapTestActivity extends Activity {
    TextView result;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        result = (TextView)findViewById(R.id.result);

        final String NAMESPACE = "http://sample.com/";
        final String METHOD_NAME = "SayHello";    
        final String SOAP_ACTION = "http://sample.com/SayHello";
        final String URL = "http://192.168.1.35/HelloAndroid/Service1.asmx";

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);            
            SoapSerializationEnvelope envelope = new      SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            String resultValue = response.toString();

            result.setText(resultValue);          
        }
        catch (Exception e) {
            result.setText(e.getMessage());
        }
    }
}

感谢您的时间

1 个答案:

答案 0 :(得分:0)

首先,您要从服务器计算机上的Internet浏览器通过http://192.168.1.35/HelloAndroid/Service1.asmx检查对Web服务的直接访问。 如果没关系,那么尝试纠正客户端(soap请求和响应)代码。 否则,请按照此link首先访问服务器。 如果存在问题,试试这个: 从控制面板注册框架:

程序和功能&gt;打开或关闭Windows功能&gt;信息信息服务&gt;万维网服务&gt; <应用程序开发特征>选择ASP.NET 4.5(您使用的版本)

单击“确定”。

完成!