简体   繁体   English

从Play商店Xamarin获取最新的应用程序版本

[英]Getting latest app version from play store xamarin

How can I get latest android app version from Google play store? 如何从Google Play商店获取最新的android应用版本? Earlier to used to do so by using below code 以前通过使用以下代码来做到这一点

using (var webClient = new System.Net.WebClient())
{
    var searchString = "itemprop=\"softwareVersion\">";
    var endString = "</";
    //possible network error if phone gets disconnected
    string jsonString = webClient.DownloadString(PlayStoreUrl);
    var pos = jsonString.IndexOf(searchString, StringComparison.InvariantCultureIgnoreCase) + searchString.Length;
    var endPos = jsonString.IndexOf(endString, pos, StringComparison.Ordinal);
    appStoreversion = Convert.ToDouble(jsonString.Substring(pos, endPos - pos).Trim());
    System.Diagnostics.Debug.WriteLine($"{currentVersion} :: {appStoreversion}");
    System.Diagnostics.Debug.WriteLine($"{appStoreversion > currentVersion}");
    if ((appStoreversion.ToString() != currentVersion.ToString() && (appStoreversion > currentVersion)))
    {
        IsUpdateRequired = true;
    }
}

& the code below even throwing exception 和下面的代码甚至引发异常

  var document = 
    Jsoup.Connect("https://play.google.com/store/apps/details?id=" + "com.spp.in.spp" + "&hl=en")
    .Timeout(30000)
    .UserAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
    .Referrer("http://www.google.com")
    .Get();

Eception: Eception:

Android.OS.NetworkOnMainThreadException: Exception of type 'Android.OS.NetworkOnMainThreadException' was thrown. Android.OS.NetworkOnMainThreadException:引发了类型为“ Android.OS.NetworkOnMainThreadException”的异常。 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

But now Play store seems to change few conditions, so existing functionality is broke down. 但是现在Play商店似乎几乎没有什么变化,因此现有功能遭到了破坏。 Few similar threads are already available here however those seems to have outdated. 这里很少有类似的线程可用,但是这些线程似乎已经过时了。

This will return a string-based version, at least until Google changes the html page contents again. 这将返回基于字符串的版本,至少直到Google再次更改html页面内容为止。

var version = await Task.Run(async () =>
{
    var uri = new Uri($"https://play.google.com/store/apps/details?id={PackageName}&hl=en");
    using (var client = new HttpClient())
    using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
    {
      request.Headers.TryAddWithoutValidation("Accept", "text/html");
      request.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
      request.Headers.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
      using (var response = await client.SendAsync(request).ConfigureAwait(false))
      {
            try
            {
                response.EnsureSuccessStatusCode();
                var responseHTML = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                var rx = new Regex(@"(?<=""htlgb"">)(\d{1,3}\.\d{1,3}\.{0,1}\d{0,3})(?=<\/span>)", RegexOptions.Compiled);
                MatchCollection matches = rx.Matches(responseHTML);
                return matches.Count > 0 ? matches[0].Value : "Unknown";
            }
            catch
            {
                return "Error";
            }
          }
      }
   }
);
Console.WriteLine(version);

Based from this link , this exception is thrown when an application attempts to perform a networking operation on its main thread. 基于此链接 ,当应用程序尝试在其主线程上执行联网操作时,将引发此异常。 You may refer with this thread wherein it stated that network operations on Android need to be performed off the main UI thread. 您可以参考该线程,其中指出需要在主UI线程之外执行Android上的网络操作。 The easiest way is use a Task to push it onto a thread in the default threadpool. 最简单的方法是使用“ Task将其推送到默认线程池中的线程上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM