2

I have been a tutorial to enable web service to fetch data from url. However, in the tutorial, it asks to add System.Json. I have added using System.Json, but it gives me an error.

// Here is the code that I have done in C# and it works very well

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft;
using Newtonsoft.Json;

namespace Weather_App
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("http://api.wunderground.com/api/key/conditions/q/CA/Houston.json");
            WebRequest webRequest = WebRequest.Create(uri);
            WebResponse response = webRequest.GetResponse();
            StreamReader stramreader = new StreamReader(response.GetResponseStream());
            String responseData = stramreader.ReadToEnd();


            var outObject = JsonConvert.DeserializeObject<Component.RootObject>(responseData);


            Console.WriteLine(responseData);
            Console.ReadLine();
        }
    }
}

// Here is the code that I would like to implement same functionality in Xamarin, but having difficulty in using system.json

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Xamarin.Forms;
using System.Xml;
using System.Json;

namespace Exercise21
{   
    public partial class MyPage : ContentPage
    {   
        public MyPage ()
        {
            InitializeComponent ();
            CallWebService ();

        }

        private async void CallWebService()
        {

            Uri url = new Uri ("http://api.wunderground.com/api/key/conditions/q/CA/Houston.json");
            var httpReq = (HttpWebRequest)HttpWebRequest.Create (url);
            httpReq.BeginGetResponse ((ar) => {
                var request = (HttpWebRequest)ar.AsyncState;
                using (var response = (HttpWebResponse)request.EndGetResponse (ar)) {                           
                    var s = response.GetResponseStream ();
                    var j = (JsonObject)JsonObject.Load (s);
                    var results = (from result in (JsonArray)j ["results"]
                                   let jResult = result as JsonObject
                                   select jResult ["text"].ToString ()).ToArray ();
                    RunOnUiThread (() => {
                        ListAdapter = new ArrayAdapter<string> (this,
                            Resource.Layout.TweetItemView, results);
                    });
                }
            }, httpReq);

        }
    }
}

enter image description here

casillas
  • 16,351
  • 19
  • 115
  • 215
  • Do you have the relevant assembly referenced? The assemblies are a little different in Xamarin than in regular .Net. [This link](http://developer.xamarin.com/guides/android/under_the_hood/assemblies/) might help. – Matt Burland Oct 07 '14 at 20:04
  • 2
    I'd avise you to use Json.NET instead. It's a library you can find on nuget – Pedro Oct 07 '14 at 20:05
  • Also, if your tutorial is online, it might be helpful to include a link to it in your question. Is it actually a Xamarin tutorial? – Matt Burland Oct 07 '14 at 20:10
  • @Pedro: I'd agree that Json.NET is a really nice library. But it actually looks like the OP already has it. Notice the `using Newtonsoft.Json;` – Matt Burland Oct 07 '14 at 20:11
  • I have also added my code . – casillas Oct 07 '14 at 20:15
  • Right, good catch, @MattBurland! – Pedro Oct 07 '14 at 22:54
  • What happens if you simply delete that using statement? – Miha Markic Oct 08 '14 at 07:09
  • @MihaMarkic, then how would you fetch the data. Example var j = (JsonObject)JsonObject.Load (s); – casillas Oct 08 '14 at 16:22
  • It is just that it doesn't look Json.net to me. Can you try something like this instead? [json.net deserialize](http://stackoverflow.com/questions/8157636/can-json-net-serialize-deserialize-to-from-a-stream) – Miha Markic Oct 08 '14 at 16:37
  • http://stackoverflow.com/questions/26303074/parsing-data-in-xamarin-forms – casillas Oct 10 '14 at 15:42

4 Answers4

1

Right Click on Referenes, then Click Add Refernce. In the Search box type "System.Json". Then click the checkbox beside it.

0

Correct me if I´m wrong, you need the JsonObjectRequest equivalent for .NET, am I right?

Check the documentation of the library you are using

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
tkruise
  • 3
  • 7
-1
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
       {
           if (response.StatusCode != HttpStatusCode.OK)
               Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
           using (StreamReader reader = new StreamReader(response.GetResponseStream()))
           {
               var content = reader.ReadToEnd();
               var Results = JsonConvert.DeserializeObject<dynamic>(content);

               if (string.IsNullOrWhiteSpace(content))
               {
                   Console.Out.WriteLine("Response contained empty body...");
               }
               else
               {
                   Console.Out.WriteLine("Response Body: \r\n {0}", content);
               }
           }
       }

can you try Json.Net add reference then try to Using jsonConvert

i hope this is help you

jeshal
  • 19
  • 3
-1

Add the JSON.net nuget packages to all of your projects. Then you can call

            string jsonAsString = jsonStream.ReadToEnd();

            RootObject theRoot = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonAsString);
MattyMerrix
  • 10,793
  • 5
  • 22
  • 32