1

I'm working on a Xamarin.Forms project and in the project I have to GET data from a Json URL, I'm using this code :

 string URL = "https://example.com/.json"; var httprequest = (HttpWebRequest)WebRequest.Create(URL); var response = (HttpWebResponse)httprequest.GetResponse(); var stream = new StreamReader(response.GetResponseStream()).ReadToEnd(); var firebasevariable = JObject.Parse(stream); string dist = firebasevariable["distance"].ToObject<string>(); 

but the "dist" value keeps returning NULL ! I imported the System.Net.Http and the Newtonsoft.Json libraries I also get this warning but I don't know if its the cuz why I get the NULL , I tried my link using the postman and it returns the data perfectly

There was a conflict between "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"

4
  • have you verified that stream contains valid JSON? Commented Mar 13, 2019 at 0:56
  • @Jason , yes the JSON is 100% valid , I tried another code but it also return's a NULL [ HttpClient client = new HttpClient(); string response = await client.GetStringAsync("https:/example.com/.json"); var variables = JsonConvert.DeserializeObject<myclass>(response); ] Commented Mar 13, 2019 at 14:45
  • try firebasevariable.Value<string>("distance"); if that doesn't work, you need to inspect firebasevariable with the debugger Commented Mar 13, 2019 at 14:48
  • @Jason i tried it but it didnt work then i tried to debug the code and i found out that the response from the JSON is already NULL , I don't know why since i tried the link with postman and its returning a valid data Commented Mar 13, 2019 at 15:06

1 Answer 1

1

I'm using Firebase too and this is working:

async Task<string> Get(string url) { using(var requestMessage = new HttpRequestMessage(HttpMethod.Get, url)) { // Client is a static HttpClient property. // It's recommended to use a single instance in all your requests, but create it in this method if you want. var response = await Client.SendAsync(requestMessage); var contentString = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) return contentString; else throw new Exception(contentString); } } 

This method will, of course, return a JSON string. After getting it, you can deserialize it to whatever you want.

If the exception is thrown, it probably means authentication issues (just read what contentString says). If it returns correctly but the value is null, then probably you are using a different URL from your Postman tests and the path's value on the database is really null.

Hope it helps!

Sign up to request clarification or add additional context in comments.

4 Comments

you are a real lifesaver ! , it worked perfectly , do you know how can I keep updating the result every one sec or two ? cuz the database I'm working with keep's updating the values every one sec , I tried to add a while loop but it didn't work
Glad to help! About your question, I really wouldn't recommend doing this in a large scale, it will be pretty expensive when the user base starts to grow. My recommendation is: if you are using Firebase Realtime Database, you can use "FirebaseDatabase.net", a library for Xamarin.Forms that has Realtime updates; if you are using Firestore and your app is only for iOS and Android, you can use the native Firestore libraries (it will be a little more complex); and if you target more than these two platforms, then you'll have to use a loop as Firestore doesn't have RESTful realtime updates yet.
If you either are on the last category that I spoken of and really can't use the native Realtime functionality of Firebase or your project will not be used by a lot of users and you think you don't need to worry about database calls, then I suggest you open another question on SO with the details of this loop that didn't work (because my method should have worked inside a loop :)
Thank you very much ! , this is the kind of information I was looking for , you've summarized the whole thing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.