首页 > 解决方案 > 为什么真实设备上的xamarin android应用程序总是抛出错误但在模拟器上工作

问题描述

我有一个奇怪的问题。我一直在使用 xamarin 表单开发一个应用程序,该表单在后端使用用 php 编写的 API。该应用程序的 iOS 版本运行良好,Android 版本在模拟器上运行良好。当我在真实设备上存档并安装 apk 文件时,它总是会为所有 api 函数抛出错误。找不到原因,哪里出错了。

仅在发布模式下使用 armeabi-v7a 和链接选项将我的 apk 文件归档为用户和 Sdk 程序集。

我是否缺少为归档项目而设置的任何属性?

任何帮助,将不胜感激。

        public async void OnLogin()
        {               
          string url = "http://www.example.com/UserController/login";

            HttpClient client = new HttpClient();

            object userjson = new
            {
                email = Email.Text.ToLower(),
                password = Password.Text
            };

            var myContent = JsonConvert.SerializeObject(userjson);
            Console.WriteLine("Post Content" + myContent);
            var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
            var byteContent = new ByteArrayContent(buffer);
            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            Console.WriteLine("Inside Login() Method");
            Console.WriteLine("Json Content::" + myContent);
            Console.WriteLine("Json Byte::" + byteContent.ReadAsStringAsync());
            var response = await client.PostAsync(url, byteContent).ConfigureAwait(false);

            var code = response.StatusCode;

            Console.WriteLine("Code::" + code);

            if (code.ToString() == "OK")
            {
                string res = response.Content.ReadAsStringAsync().Result;
                App.serverToken = "activated";
                App.Current.Properties["Token"] = App.serverToken;
                await Application.Current.SavePropertiesAsync();
                isValid = true;
                Console.WriteLine("Token::" + App.serverToken);
               }
                else
                {
                isValid = false;
                Device.BeginInvokeOnMainThread(() =>
                {
                    LoginFaild.Text = "InCorrect Email or Password";
                    Email.Text = string.Empty;
                    Password.Text = string.Empty;
                });
              }
            }

这是使用 Postasync 访问 API 的方法。

标签: androidxamarinarchiving

解决方案


推荐阅读