2012-09-24 131 views
1

我正面臨WP7中的編碼問題。我向API發出json請求,響應中包含德語元音變音,但由於某種原因,它顯示不正確。 這就是代碼發出請求:WP7變音符號和特殊字符

public static void makeRequest(string requestString,List<String> parameters,Action<string> handleRequestResult) 
    { 
     //generate a random nonce and a timestamp 
     Random rand = new Random(); 
     Random rand2 = new Random(); 
     string nonce1 = rand.Next().ToString(); 
     string nonce2 = rand2.Next().ToString(); 
     string nonce = nonce1 + nonce2; 
     string timestamp = GetTimestamp(); 

     //create the parameter string in alphabetical order 
     //string parameters = "oauth_callback=" + UrlHelper.Encode("http://www.example.com"); 
     parameters.Add("oauth_consumer_key=" + ConsumerKey); 
     parameters.Add("oauth_nonce=" + nonce); 
     parameters.Add("oauth_signature_method=HMAC-SHA1"); 
     parameters.Add("oauth_timestamp=" + timestamp); 
     parameters.Add("oauth_version=1.0"); 

     //sorting the list of parameters (adding '&' between them) 
     string sortedParameters = sortParams(parameters); 

     //generate a signature base on the current requeststring and parameters and adding it to request string 
     string signature = generateSignature("GET", requestString, sortedParameters); 
     string url = requestString + "?" + sortedParameters + "&oauth_signature=" + signature; 

     //test the request 
     WebClient web2 = new WebClient(); 
     web2.Encoding = UTF8Encoding.UTF8; 
     //string result = web.DownloadString(url); 
     web2.DownloadStringCompleted += (sender, e) => 
     { 
      string result = (string)e.Result; 
      handleRequestResult(result); 


      //App.ViewModel.LoadData(result); 
     }; 
     web2.DownloadStringAsync(new Uri(url)); 
    } 

這裏是一個示例代碼加載到視圖的響應:

public void LoadBlogPosts(string content) 
    { 

     if (!AddToExistingBlogs) 
     { 
      this.BlogPosts.Clear(); 
     } 


     XDocument xml = XDocument.Parse(content); 
     foreach (XElement element in xml.Descendants("item")) 
     { 
      BlogViewModel newBlog = new BlogViewModel(); 
      newBlog.Title = element.Element("title").Value; 
      newBlog.Description = element.Element("description").Value; 
      newBlog.Link = element.Element("link").Value; 
      newBlog.Category = element.Element("category").Value; 
      //newBlog.Comments = element.Element("comments").Value; 
      newBlog.PubDate = element.Element("pubDate").Value; 
      XNamespace dc = "http://purl.org/dc/elements/1.1/"; 
      XNamespace wfw = "http://wellformedweb.org/CommentAPI/"; 
      XNamespace atom = "http://www.w3.org/2005/Atom"; 
      XNamespace sy = "http://purl.org/rss/1.0/modules/syndication/"; 
      XNamespace slash = "http://purl.org/rss/1.0/modules/slash/"; 
      XNamespace contentn = "http://purl.org/rss/1.0/modules/content/"; 
      newBlog.Dccreator = element.Element(dc + "creator").Value; 
      newBlog.Guid = element.Element("guid").Value; 
      newBlog.WfwCommentRss = element.Element(wfw + "commentRss").Value; 
      newBlog.SlashComments = element.Element(slash + "comments").Value; 
      newBlog.Content = "<html><body style='color: white; background-color:Black'>"; 
      newBlog.Content += element.Element(contentn + "encoded").Value; 
      newBlog.Content += "</body></html>"; 

      // 
      //Extract images from post: 
      // 
      // Size the control to fill the form with a margin 

      var reg = new Regex("src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))(?:\"|\')?"); 
      var match = reg.Match(newBlog.Content); 
      if (match.Success) 
      { 
       newBlog.FeaturedImage = match.Groups["imgSrc"].Value; 
      } 
      else 
      { 
       newBlog.FeaturedImage = @"http://karkur.com/no_image.png"; 
      } 



      BlogPosts.Add(newBlog); 

     } 

    } 

這是我得到的結果是: enter image description here

基本上我需要的是在WP7中使用unicode字符的東西: convert this:enter image description here

到正常字符

回答

0

您應該將您的編碼設置爲ISO-8859-1。所以,在你的情況下:

web2.Encoding = Encoding.GetEncoding("ISO-8859-1"); 
+0

謝謝,但它沒有改變任何東西! –

+0

您的手機的語言設置是否設置爲德語? –

+0

無關緊要,當我使用XML進行響應時顯示德語,所以不存在問題。 –