1

我打算将应用程序从 Monotouh 重写为适用于 Android 的 Monodroid 应用程序。如我错了请纠正我。逻辑与 MonoTouch 中的逻辑保持一致或改变什么?如果有什么变化,请告诉我,什么?

据我了解,只有 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;

    }

            }
}

}

4

1 回答 1

4

我认为你应该阅读这个
简而言之 - 您可以重用不依赖于平台特定部分的应用程序逻辑,因此可以在 MonoTouch 和 Mono for Android 之间共享使用数据库/服务器。

于 2012-12-19T11:45:24.223 回答