0

I'm quite new to Android and WebServices and at the moment I'm reading many info about Web Services, SOAP, etc.

I'm trying to write a Web Service example that just says Hello and it works in my browser but from my Android device/emulator doesn't work. I have set its IP to my PC ipv4 address and I have turned off the firewall and the antivirus. However if in the browser I try to connect to this IP it says 404. (I don't know if this is normal) I'm also using local IIS with the following URL: http://localhost/HelloAndroid .

Here is the code of my Web Service:

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

Android Activity:

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());
        }
    }
}

Thank you for your time

4

1 回答 1

0

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

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

单击确定。

完毕!

于 2017-08-04T20:55:21.003 回答