2012-06-08 18 views
0

我有這個代碼的問題,我需要將此Json httprequest轉換爲異步方法,以便在WP7 C#應用程序中使用它,這裏是代碼,我真的需要幫助在這一個。轉換代碼c#sync httprequest async

它示出了在下面的語句中的錯誤:

1)myReq.ContentLength = postData.Length; - >的ContentLength不包含這種類型的方法

2)WebResponse的=(HttpWebResponse)myReq的。的GetResponse(); - >是一種同步方法,不包含在httpresponse metod上

3)StreamWriter stOut = new StreamWriter(myReq.GetRequestStream(),System.Text.Encoding.ASCII); - >是一種同步方法,不包含在httpresponse metod上

4)GoogleCell cell = JsonConvert.DeserializeObject(Response); - >方法重載或缺少轉換....並且轉換是在第3次錯誤。

/// internal google cell info to post 

    public GoogleCell GetCellInfo(string lac, string mnc, string mcc, string cellID) 
    { 

     HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("https://www.google.com/loc/json"); 
     myReq.Method = "POST"; 
     myReq.ContentType = "application/jsonrequest"; 
     string postData = "{\"cell_towers\": [{\"location_area_code\": \"" + lac + "\", \"mobile_network_code\": \"" + mnc + "\", \"cell_id\": \"" + cellID + "\", \"mobile_country_code\": \"" + mcc + "\"}], \"version\": \"1.1.0\", \"request_address\": \"true\"}"; 
     myReq.ContentLength = postData.Length; 

     StreamWriter stOut = new StreamWriter(myReq.GetRequestStream(), System.Text.Encoding.ASCII); 
     stOut.Write(postData); 
     stOut.Close(); 

     HttpWebResponse webresponse; 
     webresponse = (HttpWebResponse)myReq.GetResponse(); 
     Encoding enc = System.Text.Encoding.UTF8; 
     StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); 

     string Response = loResponseStream.ReadToEnd(); 
     loResponseStream.Close(); 
     webresponse.Close(); 

     GoogleCell cell = JsonConvert.DeserializeObject(Response); 
     return cell; 


     } 
    } 

回答

1

轉換在WCF完整的類仍然有

JsonConvert.DeserializeObject(響應)錯誤;

+0

你的'迴應'是什麼?你面臨什麼錯誤? – nkchandra