【问题标题】:Nothing happening when calling a .NET web service in Android在 Android 中调用 .NET Web 服务时没有任何反应
【发布时间】:2012-08-11 01:02:43
【问题描述】:

我在 .net 中开发了一个 Web 服务,在这里

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class DataManipulationService : System.Web.Services.WebService
{
    [WebMethod]
    public void InsertData()
    {
        SQLConnectionManager conn = new SQLConnectionManager();

        string qry = "INSERT INTO TEST_TABLE(Id, Name) VALUES(3, 'Ibbi')";

        SQLOperationManager op = new SQLOperationManager(qry);
        int rowsAffected = op.ExecuteQuery();
    }
}

我正在尝试从 android 调用这个 web 方法,像这样

public class WebDataManipulator {

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://localhost/DataManipulationService.asmx?WSDL";
private final String METHOD = "InsertData";
private final String SOAP_ACTION = "http://localhost/DataManipulationService.asmx?op=InsertData";

public void insertData() {
    SoapObject request = new SoapObject(NAMESPACE, METHOD);

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

    System.out.println(request);
    HttpTransportSE androidHttp = new HttpTransportSE(URL);


    try {
        androidHttp.call(SOAP_ACTION, envelope);
    } catch(Exception ex) {
        ex.printStackTrace();
    }
}

}

我已经在 .net 中测试了我的 web 服务,它工作正常,但是当我在 android 上运行它时,什么都没有发生.. 我没有得到任何异常,也没有它的功能。为什么? 这是我的日志猫

08-11 11:09:12.269: I/AndroidRuntime(325): NOTE: attach of thread 'Binder Thread #3' failed
08-11 11:09:13.179: I/System.out(333): InsertData{}
08-11 11:09:13.229: W/System.err(333): java.net.SocketException: Permission denied
08-11 11:09:13.239: W/System.err(333):  at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
08-11 11:09:13.239: W/System.err(333):  at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
08-11 11:09:13.239: W/System.err(333):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
08-11 11:09:13.249: W/System.err(333):  at java.net.Socket.checkOpenAndCreate(Socket.java:802)
08-11 11:09:13.249: W/System.err(333):  at java.net.Socket.connect(Socket.java:948)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
08-11 11:09:13.249: W/System.err(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
08-11 11:09:13.249: W/System.err(333):  at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
08-11 11:09:13.249: W/System.err(333):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:146)
08-11 11:09:13.249: W/System.err(333):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
08-11 11:09:13.249: W/System.err(333):  at com.automated.research.WebDataManipulator.insertData(WebDataManipulator.java:27)
08-11 11:09:13.259: W/System.err(333):  at com.automated.research.DataSender$1.run(DataSender.java:47)
08-11 11:09:13.259: I/System.out(333): connected

这就是我编写客户端代码的方式

if(isNetworkAvailable()) {
   WebDataManipulator webDM = new WebDataManipulator();
   webDM.insertData();
   System.out.println("connected");
}

【问题讨论】:

  • @t0mm13b 感谢您的回复,我已经编辑了我的问题并包含了 logcat。

标签: android


【解决方案1】:

webserviceurl 以“localhost”开头,这是从 android 的角度来看 android 设备。将 localhost 替换为 androiddevice 可以看到的 ip 地址(即可以从 android web 浏览器调用)

【讨论】:

  • 嗨@k3b 感谢您的回答。我尝试用我的计算机的 IP 地址替换 localhost,但现在得到以下异常 java.net.socketexception address family not supported
  • 你能从你的安卓浏览器调用webservicehttp://servername/DataManipulationService.asmx?WSDL吗?
  • no k3b 它没有从 android 浏览器调用 :( 但是当我用 ip '10.0.2.2' 替换 'localhost' 并在模拟器上运行时它工作得很好。但它不是在设备上工作
  • 我的电脑的 ip 是 192.168.5.64 我试着把它代替 'localhost' 但它在设备上不起作用。在模拟器 10.0.2.2 上工作正常
  • 这里的问题是——IP是公有的还是私有的?
【解决方案2】:

我会在这里回答我自己的问题,我正在编写我为使其正常工作所做的步骤。

  1. 确保您在 IIS 中正确托管了 Web 服务。你可以参考这个link

    托管服务后,如果您发现您的计算机上无法访问该服务,请执行第 2 步。

  2. 你需要在这里做两件事,

    1. 让 asp.net 出现在 IIS 管理器中。请参考link here
    2. 修改 web.config 文件并添加 httpGet 和 httpPost。 Example here
  3. 允许服务从远程计算机调用其 Web 方法。参考link here

  4. 确保防火墙没有阻止访问您的计算机。您可以定义新规则或禁用防火墙以查看是否有效。

按照上面的步骤,我现在可以在安卓设备上调用我的服务了。

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多