1

I have developed a web service in .net, here it is

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

and I'm trying to call this web method from android, like this

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

}

I have tested my web service in .net and its working fine but when I run this on android NOTHING HAPPENS.. neither I'm getting any exception nor its functioning. Why? This is my logcat

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

and this is how I write the client code

if(isNetworkAvailable()) {
   WebDataManipulator webDM = new WebDataManipulator();
   webDM.insertData();
   System.out.println("connected");
}
Ibad Baig
  • 2,206
  • 4
  • 20
  • 27

2 Answers2

1

the webserviceurl start with "localhost" which is from android point of view the android device. replace localhost with an ip-adress that the androiddevice can see (i.e. is callable from the android web-browser)

k3b
  • 14,079
  • 6
  • 49
  • 84
  • hi @k3b thanks for your answer. I tried replacing the localhost with my computer's ip address but now getting the following exception java.net.socketexception address family not supported – Ibad Baig Aug 11 '12 at 07:04
  • can you call the webservice `http://servername/DataManipulationService.asmx?WSDL` from your android browser? – k3b Aug 11 '12 at 07:28
  • no k3b its not being called from the android browser :( however when I replace the 'localhost' with the ip '10.0.2.2' and run on the emulator then it works just fine. But its not working on the device – Ibad Baig Aug 11 '12 at 07:56
  • my computer's ip is 192.168.5.64 i tried putting it instead of 'localhost' but it didn't work on the device. On the emulator 10.0.2.2 works fine – Ibad Baig Aug 11 '12 at 08:28
  • the problem here is this - is the IP public or private? – t0mm13b Aug 11 '12 at 13:32
  • @t0mm13b, both ips are private – Ibad Baig Aug 11 '12 at 17:54
  • Can android handset access the private ip network address and its webserver also? – t0mm13b Aug 11 '12 at 18:23
  • yes, both my pc and android device can ping each other. I tried to ping my device from command line on the pc and it was successful, i also pinged my pc from device and the reply was received. – Ibad Baig Aug 11 '12 at 18:47
  • Finally, I have managed to access the webservice from the browser on my device, I figured that the firewall was blocking the access. But still not able to call the webservice from the code. – Ibad Baig Aug 12 '12 at 01:53
  • and now finally I have resolved my issue. I will be posting my answer soon. Thanks to t0mm13b & k3b though! – Ibad Baig Aug 12 '12 at 03:52
  • I have the same problem. Web service is accessible in device browser but not in code! Can you post the answer please? – ondermerol Aug 21 '15 at 11:17
0

I would answer my own question here, I am writing the steps I did to get it working.

  1. Make sure you hosted the webservice in IIS correctly. You can refer this link.

    After hosting the service if you see the service is not accessible on your machine then follow step 2.

  2. You will need to do two things here,

    1. Make asp.net appear in IIS manager. Refer the link here.
    2. Modify the web.config file and add httpGet and httpPost. Example here
  3. Allow the service to call its webmethods from the remote machine. Refer the link here.

  4. Make sure the Firewall is not blocking the access to your computer. You can define new rules or perhaps disable the firewall to see that works.

By following the steps above, I can now call my service on the android device.

Community
  • 1
  • 1
Ibad Baig
  • 2,206
  • 4
  • 20
  • 27