I'm still a beginner and have this question:
How can i get data from a web url in my xamarin.android app?
A easy http request...
can someone help me, please?
Thank you
There are lots of solutions but you could just use the out of the box HttpClient. Here is an example I through together as an example. It probably won't compile but should give you the idea.
using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; public class WebInterface { private HttpClient _client; public WebInterface(HttpClient httpClient) { _client = httpClient; } public async Task<T> MakeGetRequest<T>(string resource) { try { var request = new HttpRequestMessage() { RequestUri = new Uri(_client.BaseAddress, resource), Method = HttpMethod.Get, }; var response = await _client.SendAsync(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var responseString = await response.Content.ReadAsStringAsync(); var model = await DeserializeObject<T>(responseString); return model; } else if (response.StatusCode == HttpStatusCode.Unauthorized) { // you need to maybe re-authenticate here return default(T); } else { return default(T); } } catch (Exception e) { throw e; } } }
The resource parameter is your url. Just make sure the RequestUri parameter of the HttpRequestMessage is set to your url.
From your app you might call it like this:
var result = await MakeGetReqeust("https://www.apple.com");
I was assuming you were trying to call and web service endpoint. What is it your are trying to do?
If you are trying to just display a webpage, then you would want to use a WebView on your page.
If you don't understand the code above, I am guessing it is the async/await mechanism. I would suggest spending time learning what those are as they are key to doing any Xamarin development.
Answers
Take a look to RestServices: Consuming a RESTful Web Service. Did this web have an API endpoint to connect?
There are lots of solutions but you could just use the out of the box HttpClient. Here is an example I through together as an example. It probably won't compile but should give you the idea.
Sorry, but i dont understand the code
Where can i place my url?
And how can i use it in my xamarin.android App?
The resource parameter is your url. Just make sure the RequestUri parameter of the HttpRequestMessage is set to your url.
From your app you might call it like this:
var result = await MakeGetReqeust("https://www.apple.com");
I was assuming you were trying to call and web service endpoint. What is it your are trying to do?
If you are trying to just display a webpage, then you would want to use a WebView on your page.
If you don't understand the code above, I am guessing it is the async/await mechanism. I would suggest spending time learning what those are as they are key to doing any Xamarin development.
Yes, you are right i try to call an web service end point...
And thank you for the answer