2012-12-19 40 views
1

我將重寫從Monotouh到Android應用程序的Monodroid應用程序。如我錯了請糾正我。邏輯與MonoTouch中的邏輯保持一致還是改變了什麼?如果有什麼變化,請告訴我,什麼?從MonoTouch應用程序重寫爲MonoDroid

據我瞭解,只有GIU變化。提前致謝!

所以,這是我的代碼,我從我的服務器調用數據:

namespace Mobile{ 
public static class SiteHelper 
{ 
    public static string DbPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "Sql_1.4.sqlite"); 
    public const string TempDbPath = "./Sql.sqlite"; 


    public static UIView View { get; set; } 

    public static BaseController Controller { get; set; } 

    private static event NIHandler _noInternetHandler; 

    private static bool _noInternetShoved = false; 
    public static string SiteDomain = "http://mysite.com"; //files which connecting to the DB on server (.asx files) 

    private delegate void NIHandler(); 



    public static XDocument DoRequest (string Request) 
    { 

     if (_noInternetHandler != null) { 
      foreach (var del in _noInternetHandler.GetInvocationList()) { 
       _noInternetHandler -= del as NIHandler; 
      } 
     } 

     if (Controller != null) 
      _noInternetHandler += new NIHandler (Controller.PushThenNoInternet); 

     string CryptoString = ""; 
     string Language = "ru"; 

     using (MD5 md5Hash = MD5.Create()) { 
      string hashKey = Guid.NewGuid().ToString().Substring (0, 4); 
      CryptoString = Request + (Request.Contains ("?") ? "&" : "?") + "hash=" + GetMd5Hash (
       md5Hash, 
       "myprogMobhash_" + hashKey 
      ) + "&hashKey=" + hashKey + "&language=" + Language; 




      UIActivityIndicatorView _preloader = null; 

      if (Controller != null) { 
       Controller.InvokeOnMainThread (delegate() { 
        _preloader = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.Gray); 


        if (View != null && Request.IndexOf ("login.ashx") == -1 
         && Request.IndexOf ("yandex") == -1 
         && Request.IndexOf ("GetDialogMessages") == -1) { 

         lock (_preloader) { 
          if (_preloader != null && !_preloader.IsAnimating) 
           _preloader.HidesWhenStopped = true; 
          _preloader.Frame = new RectangleF (150, 170, 30, 30); 
          _preloader.Transform = MonoTouch.CoreGraphics.CGAffineTransform.MakeScale ((float)1.3, (float)1.3); 
          _preloader.StartAnimating(); 
          View.Add (_preloader); 
         } 
        } 
       }); 
      } 


      /*ctx.GetText(Resource.String.SiteAddress)*/ 
      Stream Stream = null; 
      try { 
       HttpWebRequest request = new HttpWebRequest (new Uri (SiteDomain + "/FolderWithFiles/" + CryptoString)); 
       request.Timeout = 8000; 
       Stream = request.GetResponse().GetResponseStream(); 
       _noInternetShoved = false; 
       if (_noInternetHandler != null) 
        _noInternetHandler -= new NIHandler (Controller.PushThenNoInternet); 
      } catch (WebException) { 
       if (_noInternetHandler != null) 
        _noInternetHandler.Invoke(); 
       var resp = new XDocument (new XElement ("Response", 
          new XElement ("status", "error"), 
          new XElement ("error", "Отсутствует интернет")) 
       ); 
       return resp; 

      } 


      StreamReader Sr = new StreamReader (Stream); 
      string Resp = Sr.ReadToEnd(); 
      XDocument Response = XDocument.Parse (Resp.Substring (0, Resp.IndexOf ("<html>") == -1 ? Resp.Length : Resp.IndexOf ("<!DOCTYPE html>"))); 
      string Hash = Response.Descendants().Where (x => x.Name == "hash") 
       .FirstOrDefault().Value; 
      string HashKey = Response.Descendants().Where (x => x.Name == "hashKey") 
       .FirstOrDefault().Value; 

      Sr.Close(); 
      Stream.Close(); 


      if (Controller != null && _preloader != null) { 
       Controller.InvokeOnMainThread (delegate() { 
        lock (_preloader) { 
         _preloader.StopAnimating(); 
         _preloader.RemoveFromSuperview(); 
        } 
       }); 
      } 


      if (VerifyMd5Hash (
       md5Hash, 
       "mobileSitehash_" + HashKey, 
       Hash 
      )) 
       return Response; 
      else 
       throw new Exception(); 


     } 
    } 



    public static XDocument DoWriteFileRequest (string Request, byte[] file) 
    { 
     string CryptoString = ""; 
     string Language = "ru"; 

     using (MD5 md5Hash = MD5.Create()) { 
      string hashKey = Guid.NewGuid().ToString().Substring (0, 4); 
      CryptoString = Request + (Request.Contains ("?") ? "&" : "?") + "hash=" + GetMd5Hash (
       md5Hash, 
       "mobileMobhash_" + hashKey 
      ) + "&hashKey=" + hashKey + "&language=" + Language; 

      HttpWebRequest Req = (HttpWebRequest)WebRequest.Create (SiteDomain + "/misc/mobile/" + CryptoString); 
      Req.Method = "POST"; 
      Stream requestStream = Req.GetRequestStream(); 
      requestStream.Write (file, 0, file.Length); 
      requestStream.Close(); 

      Stream Stream = Req.GetResponse().GetResponseStream(); 
      StreamReader Sr = new StreamReader (Stream); 
      string Resp = Sr.ReadToEnd(); 
      XDocument Response = XDocument.Parse (Resp); 
      string Hash = Response.Descendants().Where (x => x.Name == "hash") 
       .FirstOrDefault().Value; 
      string HashKey = Response.Descendants().Where (x => x.Name == "hashKey") 
       .FirstOrDefault().Value; 

      Sr.Close(); 
      Stream.Close(); 

      if (VerifyMd5Hash (
       md5Hash, 
       "mobileSitehash_" + HashKey, 
       Hash 
      )) 
       return Response; 
      else 
       throw new Exception(); 


     } 
    } 

    public static string GetMd5Hash (MD5 md5Hash, string input) 
    { 

     // Convert the input string to a byte array and compute the hash. 
     byte[] data = md5Hash.ComputeHash (Encoding.UTF8.GetBytes (input)); 

     // Create a new Stringbuilder to collect the bytes 
     // and create a string. 
     StringBuilder sBuilder = new StringBuilder(); 

     // Loop through each byte of the hashed data 
     // and format each one as a hexadecimal string. 
     for (int i = 0; i < data.Length; i++) { 
      sBuilder.Append (data [i].ToString ("x2")); 
     } 

     // Return the hexadecimal string.2 
     return sBuilder.ToString(); 
    } 

    //Geting the info for my app 
    public static List<PackageListModel> GetUserPackages (int UserID) 
    { 
     List<PackageListModel> Events = new List<PackageListModel>(); 
     string Req = "SomeFile.ashx?UserID=" + UserID; 
     XDocument XmlAnswer = DoRequest (Req); 

     if (XmlAnswer.Descendants ("status").First().Value == "ok") { 
      foreach (var el in XmlAnswer.Descendants ("Response").First().Descendants().Where(x=>x.Name == "Event")) { 
       PackageListModel Event = null; 
       Event = new PackageListModel() 
       { 
        ID = int.Parse(el.Attribute("ID").Value), 
        Title = el.Element("Title").Value, 
        Date = el.Element("Date").Value, 
        Price = el.Element("Price").Value, 
        ImageUrl = el.Element("ImageUrl").Value, 
        Location = el.Element("Location").Value 
       }; 
       Events.Add (Event); 
      } 
     } 
     return Events; 
    } 

    //Получить пользовательские поездки 
    public static List<TransporterListModel> GetUserTransporters (int UserID) 
    { 
     List<TransporterListModel> Events = new List<TransporterListModel>(); 
     string Req = "SomeFile.ashx?UserID=" + UserID; 
     XDocument XmlAnswer = DoRequest (Req); 

     if (XmlAnswer.Descendants ("status").First().Value == "ok") { 
      foreach (var el in XmlAnswer.Descendants ("Response").First().Descendants().Where(x=>x.Name == "Event")) { 
       TransporterListModel Event = null; 
       Event = new TransporterListModel() 
       { 
        ID = int.Parse(el.Attribute("ID").Value), 
        Date = el.Element("Date").Value, 
        Price = el.Element("Price").Value, 
        TransportsStr = el.Element("Transports").Value, 
        Location = el.Element("Location").Value 
       }; 
       Events.Add (Event); 
      } 
     } 
     return Events; 

    } 

      } 
} 

}

+0

你絕對應該發佈一些代碼示例,顯示你已經嘗試過併發布具體問題以獲得答案。 –

+0

是的,但我想知道它是否有意義。我可以這樣做嗎?我正在使用SQLite數據庫並以xml的形式從服務器獲取信息。我想知道全球是否可以這樣做。 –

回答

4

我想你應該閱讀this
簡而言之 - 您可以重用不依賴平臺特定部分的應用程序邏輯,因此可以在MonoTouch和Mono for Android之間共享數據庫/服務器。

+0

很酷!))謝謝。我認爲我可以做,我要做什麼。 –