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

midhunhk
  • 5,402
  • 7
  • 49
  • 79
Katherine99
  • 971
  • 3
  • 18
  • 37
  • Hi there, my knowledge of the Android emulator isn't the greatest but if on your emulator you go to the browser and do `http://10.0.2.2/HelloAndroid` (or `http://10.0.2.2/HelloAndroid/Service1.asmx`) do you still get a 404 error? – nkvu Mar 29 '13 at 23:16
  • And make sure you read and understand this thread http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – minhaz Mar 29 '13 at 23:22
  • nkvu, I still get that error and I also added "" in the manifest – Katherine99 Mar 29 '13 at 23:24
  • Hi there, what about just accessing `http://10.0.2.2` from the web browser on your emulator? If IIS is up and running it should serve out the IIS welcome page... – nkvu Mar 29 '13 at 23:36
  • Thank you nvku but this doesn't work either – Katherine99 Mar 29 '13 at 23:43
  • Does your browser just present another 404? It's a bit strange because `10.0.2.2` should [loopback](http://developer.android.com/tools/devices/emulator.html#emulatornetworking) to your local development machine where IIS is running so I would expect at least the IIS welcome page to appear. It's not a direct solution but instead of calling your locally developed .asmx what if you follow the steps and reference the one in [this](http://www.codeproject.com/Articles/304302/Calling-Asp-Net-Webservice-ASMX-From-an-Android-Ap) article? Are you able to call that successfully? – nkvu Mar 29 '13 at 23:50

1 Answers1

0

First of all you want to check direct access to web services through http://192.168.1.35/HelloAndroid/Service1.asmx from your internet browser on the server machine. If it's okay, then try to correct client side (soap request and response) code. Else, follow this link to make access to server first. If problem exists try this: Register framework from the control panel:

Programs and Features > Turn Windows features on or off > Information Information Services > World Wide Web Services > Application Development Features > Select ASP.NET 4.5 (version you use)

Click OK.

Done!

Safvan CK
  • 909
  • 7
  • 16