Hi guys,
I have to consume a Rest Service, created using Asp.Net Web API (but this is not important), on a project with Xamarin iOS.
I know that I can use ServiceStack or RestSharp but I want to try to use HttpClient.
To deserialise response I need to use method "ReadAsAsync" contained in "System.Net.Http.Formatting" but this It's not included in Framework. I've tried to use assembly contained in Mono 3.2.3 but I have this error:
Error CS1705: Assembly System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' references
System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version number than imported assembly `System.Net.Http, Version=2.0.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' (CS1705)
I want to ask to you how properly use HttpClient to consume Rest Service?
I want simply to execute this code on my Xamarin.iOS app:
HttpResponseMessage response = client.GetAsync("api/products").Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
var products = response.Content.ReadAsAsync<IEnumerable>().Result;
foreach (var p in products)
{
Console.WriteLine("{0}\t{1};\t{2}", p.Name, p.Price, p.Category);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
I hope you help me.
Thanks!!
Posts
@mapo80 not sure if you are returning XML or Json but would suggest returning JSON and parsing with Json.net. Check out the store component.
http://components.xamarin.com/view/json.net
Yes, rest service returns JSON. Yes, now I'm using json.net with this code:
string path = "";
HttpResponseMessage response = Client.GetAsync(path).Result;
if (response.IsSuccessStatusCode)
{
var object = JsonConvert.DeserializeObject (response.Content.ReadAsStringAsync ().Result);
}
Is this right? So, it's not possible to use System.Net.Http.Formatting!
Thanks for your answer.
To use Formatting please install the PCL library Microsoft ASP.NET Web API 2 Client using Nuget.
Great, thanks! I'll try it.
@mapo80 here is what I would do:
`
Create a product item class file as follows:
This task can be made easier by using http://json2csharp.com/ and giving it the JSON output from your call or the URL for the call.
Once you have this then use the following to make the HTTP request / response calls where SearchRequest is the URL Encoded http web address of your API. Note only URL encode the parameters to the request usually this is the stuff after ¶m= in quotes:
Now using Newtonsoft.Json.Net deserialise the Product Item from the JSON response:
Hope this helps.
`
Ok, thanks for your answer.
Don't use a blocking web call. Here is an example client which uses abstracted serializers (and supports Json.NET and ServiceStack plugins):
https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Web/RestClient.cs
Great thanks.
My goal is to create a project for my rest service that I can use in my MonoDroid and MonoTouch application.
I don't want to create a project for every application. I've seen that HttpClient is platform specific (http://forums.xamarin.com/discussion/7719/using-httpclient).
I've to use pcl version of HttpClient?
HttpClient can be PCL if you don't want WP8 support. If you do, then either create an interface for the web client and then use DI at runtime. Using Microsoft's async HttpClient PCL library has been troublesome.
That is not true. I have http client set up in the same pcl project used by all 3 platforms.
In fact, it is Xamarin error messages which are buried, especially for iOS compilation (it should be better for the next release). It works perfectly with no problem. Just never install the HttpClient PCL package in your ios/android project, only in pcl projects. Just reference System.Http from mono in android/ios. And also never add the Microsoft.Bcl package in your ios/android project (ignore the warning).
can anyone can help me to retriving value from a server using json webservice
anyone can help me to retriving value from a server using json webservice
@Shamnad
I'm assuming you're asking how to communicate with a web service by both sending and receiving json.
Here's a bare bones method that should be enough for you to modify it to your needs.
You'll probably need to add references to your project for json.net and system.net.http. I'm assuming you can parse the json once it's returned.
can anyone help me to understand why when I use similar code to Richard's I get the following error:
{System.Net.WebException: Error: ConnectFailure (Connection timed out) ---> System.Net.Sockets.SocketException: Connection timed out
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in :0
at System.Net.Web...
I post the same json string to the same url using fiddler and have no problems but does not matter what i do i always get this error calling PostAsync from android.
Are you trying to call a service on a localhost? If so, try address 10.0.2.2 instead of localhost or 127.0.0.1. If not, check that you can connect to the address in question from the Android device.
Sami, thank you for your advise that got me thinking ;~).
I was 100% convinced that these is something to do with android/xamarin and portable libraries i use but it turned out the firewall on my windows.
I faced in adding the Microsoft HttpClient Libraries reference to Xamarin Studio using NuGet Pachage manager.
After a lot of digging I found out the problem is with "Target Frameworks" Build options.
If you have checked "Silverlight 4" as a target Framework for your project you may face Adding reference issue.
If this is the case with you simply go to 'Project->Options->Build->General->Under Target Framework, untick Silverlight 4 option.
Just refresh the Project & Build. It worked for me.
I am having no success with 10.0.2.2, is there something else to try?
@h8tow8 did you try in a device?
I'm getting the same error, (ConnectFailure (Connection timed out)), when using HttpClient and testing in a device.
my problem was silly - i had a firewall turned on on a pc where the web service was running ;~)
once i disabled it - all was good
Fixed.
It was a DNS setting and the IT area help me solve it.
Thanks!
hai guys,
i need to populate table view using this String i can fetch the data to an array
but how can i populate table view using that array?
this is my code in controller
i have another Class for data source
how can i populate table view using that array
i got my error ..with help of some other friends............
@MartinUshima : Can you please tell me in detail , how you have fixed it? I'm facing this connect failure issue on user device also.
@SagarPanwala, the scenario that I was facing was that the services that I was consuming were outside my company network. So, I talked with the IT guys in my company, specified them the IPs addresses and URLs that I was trying to connect to. They register that info in the allowed or trusted source and that was it.
It had nothing to do with code, just network settings.
The issue is that those settings are managed by the IT area, nothing to do with me.