No se puede asignar HttpWebResponse.GetResponseStream() a Stream

estoy obteniendo jsonstream.Length threw an exception of type System.NotSupportedException error while trying for:

 HttpWebRequest jsonHTTPRequest = (HttpWebRequest)WebRequest.Create(jsonRequestURL);
        jsonHTTPRequest.ContentType = "application/json; charset=utf-8";

        HttpWebResponse jsonHTTPResponse = (HttpWebResponse)jsonHTTPRequest.GetResponse();
        RootObject vikiRootObject = default(RootObject);

        using (Stream jsonstream = jsonHTTPResponse.GetResponseStream())
        {
            //encoding encode = system.text.encoding.getencoding("utf-8");
            DataContractJsonSerializer serializer =
                                     new DataContractJsonSerializer(typeof(RootObject));
            vikiRootObject = (RootObject)serializer.ReadObject(jsonstream);
        }

Also Tried with Webclient but still the same error. Is this because of large response size???

La URL de la solicitud es: http://www.viki.com/api/v2/channels.json

WebClient webClient = new WebClient();
                RootObject vikiChannelData = default(RootObject);
                webClient.OpenReadAsync(new Uri(jsonRequestURL), UriKind.RelativeOrAbsolute);

                webClient.OpenReadCompleted += (obj, Args) =>
                    {
                        //DataContractJsonSerializer vikiChannelerialized = new DataContractJsonSerializer(typeof(RootObject),null);
                        DataContractJsonSerializer vikiChannelerialized = new DataContractJsonSerializer(typeof(RootObject));
                        vikiChannelData = vikiChannelerialized.ReadObject(Args.Result) as RootObject;
                        Console.WriteLine();
                    };

Edit: I tried with LINQ :

var RootObjects = from vikiroot in vikiRootObject
                                  select new Thumbnails2
                                  {
                                      thumbnails = vikiRootObject.thumbnails
                                  };

But getting the error ,Could not find an implementation of the query pattern for source type object. Select not found.

My Class Structure is something like this:

  public class RootObject
    {
        public int id { get; set; }
        public string title { get; set; }
        public string description { get; set; }
        public string uri { get; set; }
        public List<Episode> episodes { get; set; }
        public Thumbnails2 thumbnails { get; set; }
        public string timestamp { get; set; }
        public List<object> genres { get; set; }
        public string origin_code { get; set; }
    }

y

public class Thumbnails2
    {
        public string c_220_160 { get; set; }
        public string c_102_102 { get; set; }
        public string c_180_130 { get; set; }
        public string c_110_80 { get; set; }
        public string xl { get; set; }
        public string large { get; set; }
        public string medium { get; set; }
        public string small { get; set; }
        public string c_320_300 { get; set; }
        public string c_640_600 { get; set; }
        public string c_95_70 { get; set; }
        public string c_190_140 { get; set; }
        public string c_280_200 { get; set; }
        public string c_560_400 { get; set; }
    }

preguntado el 28 de agosto de 12 a las 10:08

3 Respuestas

Your returned json is not a single object instead it is an array. Since you haven't posted your RootObject and other child classes (I am too lazy to declare them) I will use Json.Net y dynamic keyword

using (var wc = new WebClient())
{
    var json = wc.DownloadString("http://www.viki.com/api/v2/channels.json");
    dynamic dynObj = JsonConvert.DeserializeObject(json);

    foreach (var item in dynObj)
    {
        Console.WriteLine(item.title);
        foreach (var episode in item.episodes)
        {
            Console.WriteLine("\t" + episode.title);
        }
    }

}

EDITAR

using (var wc = new WebClient())
{
    var json = wc.DownloadString("http://www.viki.com/api/v2/channels.json");
    var rootObj = JsonConvert.DeserializeObject<RootObject[]>(json);

    var obj = rootObj.Select(r=>new 
                    {
                        Title = r.title,
                        Thumbnail = r.thumbnails.small
                    });
}

enter image description here

Respondido 29 ago 12, 07:08

Thanks , But could you please help with the LINQ expressiion to get the dare , I tried but getting the error Could not find an implementation of the query pattern for source type object. Select not found, updated the question - simsons

¿Intentaste esto?

using (StreamReader reader = new StreamReader(jsonHTTPResponse.GetResponseStream()))
        {
             //your code goes here
        }

Actually that should be in comment, but I was unable to add one.

Respondido 28 ago 12, 11:08

Try doing the following by copying it into a memory stream to be able to read the Stream:

using (Stream jsonstream = jsonHTTPResponse.GetResponseStream()) 
        { 
            DataContractJsonSerializer serializer = 
                                     new DataContractJsonSerializer(typeof(RootObject)); 

             using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonstream)))
            {
                vikiRootObject = (RootObject)serializer.ReadObject(jsonstream); 

                using (MemoryStream ms2 = new MemoryStream())
                {
                    ser.WriteObject(ms2, vikiRootObject);
                    string serializedJson = Encoding.UTF8.GetString(ms2.GetBuffer(), 0, (int)ms2.Length);
                }
            }

 } 

Respondido 28 ago 12, 13:08

When the 1st using block wont get any data why should go ahead with rest of the operation !! - simsons

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.