Hello everyone,
I am very new to mobile development and currently needs to develop an app in Android using Xamarin.Android. I have worked in ASP.NET for some time now.
Okay now to the question: I have created a MySQL database and also the prototype of the Android app. Now I need to create web service to make the two communicate. Can any of you guide me / share a tutorial / code example / article that tells how to create as well as consume web services? I don't have a preference regarding the type of web service used (REST / XML / JSON). The only aim is to be able to create web service that can let me fetch and save data on the database from my app.
Thanks,
Ahmed
What the exception means is that your webservice can not be reached. How are you running your app? On a device or on an emulator? Where is your webservice? On your local machine, on an actual server?
Think about where your app is running from and where your webservice is, and how to get a connection between the two. Example if you're connecting to "http://localhost/MyWebService" and running your app is running on a device, then of course it's going to fail. That's because "localhost" is not a valid url from an android device.
On the emulator loopback address is 10.0.2.2 IIRC. I normally just use the machine's IP address so it will work on the emulator and on the device itself.
http://developer.android.com/tools/devices/emulator.html
Network Address Description 10.0.2.1 Router/gateway address 10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine) 10.0.2.3 First DNS server 10.0.2.4 / 10.0.2.5 / 10.0.2.6 Optional second, third and fourth DNS server (if any) 10.0.2.15 The emulated device's own network/ethernet interface 127.0.0.1 The emulated device's own loopback interface
Answers
If your very familiar with ASP.Net mvc, the I'd recommend building a restful service using ASP.NET Web API. Very slick and easy to use. You can call it from your Android app the same way as any other .NET project.
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Thanks Ruben for your response but I am afraid I haven't worked on MVC. Anyway, I created a JSON WCF REST service and now I will start looking for a tutorial / article that helps me consume the service in my Xamarin.Android app.
Can you help me there?
Hello everyone,
I have been trying to consume a web service throughout the day today but in vain! I have written a WCF REST JSON web service but couldn't consumer it. Then I wrote a simple .NET ASMX web service and tried to consume it as well but to no avail. Can you share a link of a tutorial / article / video that gives a step-by-step guide to consume a web service in Xamarin.Android app?
Any help in this regard will be highly appreciated.
Thanks,
Ahmed
http://www.servicestack.net/
http://www.servicestack.net/monotouch/remote-info/
http://docs.xamarin.com/guides/cross-platform/application_fundamentals/introduction_to_web_services
Thanks Sami but can you direct me to some place where I can use the built-in things rather than adding new framework?
Ruben, this was the first link that I went through but unfortunately it didn't help me achieve the required task. So basically I am still in the middle of nowhere!
So, you have not given any type of information (error logs, stack trace, code, things that you have tried, etc). All you have said is "it doesn't work". Post exactly the problems you are having with as much detail as you can and you'll likely get the response you're looking for.
I agree Ruben, you are right. Let me go into detail.
Lets talk about the "3. Consuming SOAP Services" section of the link that you shared. I have a very simple web service running at http://localhost:27417/MathService.asmx. Here is its code:
Now I also have an Android app that I have in Xamarin Studio and I want to consume this web service in that app. The next step I do is I add the web reference (see the attached image).
This brings me to the "3.2 Invoking SOAP Services" section and this is where I am totally lost! It simply gives me two lines of code but I neither seem to have _Service nor PopulateResults(). Can you take me ahead from here?
Thanks.
Can anybody please help me out here?
You can not "add web reference". It generates code which include an unavailable NET library.
You must write your own HTTP query code as usual.
Do you mean that to consume a SOAP Service I have to follow different steps than the ones I have mentioned above? If yes, can you guide me how to do that?
Thanks.
Okay guys, I tried another thing. I used this article from MSDN to get the JSON string from my WCF service. It works flawlessly in a simple ASP.NET application but when I try to run the same code in my Xamarin.Android application, I get an error at the line
WebResponse response = request.GetResponse ();
It says 'System.Net.WebException has been thrown. Error: ConnectFailure (The requested address is not valid in this context)'
Here is the Stack Trace:
Please let me know what could be the problem here.
Thanks.
Ahmed,
Not sure if you got passed this as this is old thread. But I was trying this today and here is how it works:
By the way, I am trying out SOAP rightnow and hoping it works similarly for REST as well for you.
Piyush,
Thank you very much for coming to my help as this still stands unresolved! Unfortunately, I am still getting an exception after following the steps mentioned by you. As soon as I call the required method, I get the following exception:
This is my simple code after adding the reference just like you mentioned and I am not getting any compilation errors:
Thanks and hoping to finally resolve this issue with your help.
What the exception means is that your webservice can not be reached. How are you running your app? On a device or on an emulator? Where is your webservice? On your local machine, on an actual server?
Think about where your app is running from and where your webservice is, and how to get a connection between the two. Example if you're connecting to "http://localhost/MyWebService" and running your app is running on a device, then of course it's going to fail. That's because "localhost" is not a valid url from an android device.
On the emulator loopback address is 10.0.2.2 IIRC. I normally just use the machine's IP address so it will work on the emulator and on the device itself.
http://developer.android.com/tools/devices/emulator.html
hi, im new with xamarin, im having an error: "System.Web.Services.Protocols.SoapException:" with WCF when execute from the emulator (i change the ip to 10.0.2.2 and the namespace reference from http://tempuri.org to the ip)
i make a breakpoint an here is where it fails:
Ruben mate, where had you hidden this vital piece of information! Though it is very trivial but I was overlooking this point. I was running the app on the connected device and the web service was on my local machine and as you said "localhost" is not valid for an Android device.
As a result I asked for a relevant hosting from client and that's when Sami jumped in to make things even easier for me. The address 10.0.2.2 finally worked for me on the emulator but I cannot make it run on the device. Sami, I tried giving my machine's address 192.168.0.17 instead of 10.0.2.2 but I get Request Timed Out error.
Anyway, thanks a lot mates. I have finally managed to move things forward.
Ahmed
@elsanto079, I am sorry I won't be able to help you here as I don't know what's going on. As per my last post, I did get my thing working and I have a WCF service as well. Perhaps somebody else can help you.
Thanks,
Ahmed
Dear Tahir,
Did you get any solution? Actually, I'm also new and want to implement same thing. How I have to achieve. Kindly, guide me with sample of code.
Thanks in advance.
Shoaib, first of all sorry for the late response. Well as I wrote in one of my previous messages that I have managed to make it work on an emulator with the help of Ruben and Sami but not on actual device. What's the exact problem that you are facing?
I suggest that you write a simple web service and a simple client to consume it. Make this work first, to make sure that the communication is happening. You can later fill it in with details.
Thanks.
Dear Ahmed
Can you please help me in the same by guiding step by step..
I am also getting the same error...unable to connect android with web service...
i am working on localmachine....and emulator i also on local machine(from tools menu)
please help
Hi,
Am also trying to consume Web Service in my app, it is working fine in an emulator. But not in my device. I have generated an apk file and installed in my device(NEXUS 4, ST23i), web service cannot be called.
Note that Service URL is given as static IP
It would be much greatful if anyone help me to find the way to consume web service from my own device.
Thanks in advance.
Hi,
I think if you go through all the comments above, they should guide you towards the resolution of the issue. However, two immediate things come to my mind:
Try opening the web service URL in your device's web browser and see if it opens correctly or not.
Make sure that you have added INTERNET permission for your app in AndroidManifest.
Thanks
hi i'm currently having this challenge, i have added my asmx web service to my android app and it generate a reference.cs code please how can i call the webservice from xamarin. here is my web service
[WebMethod] public string insertData(string title, string coment) { string Message;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO Header (title, coment)
VALUES (@title, @coment)", con);
cmd.Parameters.Add("@title", SqlDbType.NVarChar, 50).Value = title;
cmd.Parameters.Add("@coment", SqlDbType.NVarChar, 50).Value = coment;
con.Open();
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = " Details inserted successfully";
}
else
{
Message =" Details not inserted successfully";
}
con.Close();
return Message;
}
very helpful
Why we are not getting response from loaclhost i.e. 10.0.2.2...? Getting error Connect failure ...
InnerException {System.Net.Sockets.SocketException: Connection timed out at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000f7] in :0 at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0019b] in :0 } System.Net.Sockets.SocketException
hopefully this will help you, I know a lot of people have difficulties working with rest services. So maybe this article will help a little bit :
http://eidand.com/2015/07/25/xamarin-rest-services-pcl/
Piece of advice, before trying anything make sure your service actually works.
I use Fiddler and Postman ( both free ) to test all my endpoints, be it GET or POST. Once you are sure your service works then try the code in the article. It will show how to build a service wrapper which works cross platform from a PCL.
Dear Eidand
I have checked your link. 2 3 things are not clear to me...
Don't have much idea about xamarin.... So if you guide me that will be very helpful for me...
both, doesn't matter where you create it, it's the same thing and the same type
How is it possible that on iOS, localhost works fine and connects perectly to correct site (laptop's site?)
@MartinFoltyn
Because the iOS Simulator is a simulator and the Android Emulator is an Emulator. The iOS Simulator is not an isolated entity. It uses the resources of your Mac. So it uses the same CPU, same RAM, same file system, same network resources, etc, all without restriction. So if you have a 16 GB MacBook Pro, the Simulator will utilize as much RAM as Mac OS X will allow it. Even though iPhone devices may be restricted to 1 GB or 2 GB of RAM. Same for the CPU. Your iOS app on the Simulator will utilize the full power of your host CPU, even though an iDevice CPU is less powerful. On the networking side, it uses the same network resources.
The Android Emulator is an isolated entity. It's basically a virtual machine that runs isolated from the host Mac/PC. So there are limits to how much CPU it can use, limits to how much RAM it can use, has its own isolated file system (that mimics the Android file system), and uses it's own network resources. Since the emulator is isolated, it knows nothing about your host PC/Mac. From the Emulator, localhost (127.0.01) will route to itself (your webservice is not running in the emulator, it's running on the host). You'll have to use 10.0.2.2, which the emulator will route your host PC/Mac.