Domanda

Sto cercando di ottenere risultati da così API in un'app WP7.Sono stato in grado di farlo funzionare in un'app console quando ho usato il seguente codice

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 parte importante era la codifica.Default.Se ho scelto qualsiasi altra cosa tornerebbe con l'intestazione Gzip Error, il primo byte magico non corrisponde "o qualcosa di simile.

WP7 non ha predefinito, ha solo UNICODE e UTF8 che non funzionano nessuno dei due.

Idee?

È stato utile?

Soluzione 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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top