Question

J'essaie d'obtenir des résultats de si API dans une application WP7.J'ai été capable de le faire fonctionner dans une application de console lorsque j'ai utilisé le code suivant

static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            Console.Clear();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
            var stream = new MemoryStream(Encoding.Default.GetBytes(e.Result));
            var gzstream = new GZipInputStream(stream);

            RootObject qs = ser.ReadObject(gzstream) as RootObject;

            foreach (Question q in qs.questions)
            {
                Console.WriteLine(q.title);
            }

        }

La partie importante était encoding.default.Si j'ai choisi autre chose, il reviendrait avec erreur GZIP en-tête, le premier octet magique ne correspond pas 'ou quelque chose de similaire.

WP7 n'a pas de valeur par défaut, il n'a que Unicode et UTF8, ce qui n'y a aucun d'entre eux.

Idées?

Était-ce utile?

La solution 2

use WebRequest.BeginGetResponse instead. This way you can get the bytes as @carlosfigueria suggested but since webclient only has getstring this is a work around.

Autres conseils

Don't use WebClient.DownloadString, use DownloadData. This way you'll receive the GZip-encoded bytes (which can't really be converted to string), and you can pass it directly to the GZupInputStream.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top