我的Xamarin跨平台应用程序在调试模式(Android)下运行良好,但是当我更改为发布版本时,它将引发异常。我已经搜索过以纠正这种情况。大多数帖子都表明,android.permission.INTERNET
需要添加到AndroidManifest.xml文件中。但这对我的问题不起作用。
此外,无论链接程序仅是Sdk程序集还是在发布模式下为“无”,它仍然无效。 (我的应用程序基于Android Pie 9.0构建)
还有例外:
Unhandled Exception:
04-21 22:37:08.972 E/mono ( 5319): System.NullReferenceException: Object reference not set to an instance of an object.
04-21 22:37:08.972 E/mono ( 5319): at .Views.LoginPage+<SigninProcedure>d__2.MoveNext () [0x001d6] in LoginPage.xaml.cs:56
04-21 22:37:08.972 E/mono ( 5319): --- End of stack trace from previous location where exception was thrown ---
04-21 22:37:08.972 E/mono ( 5319): at (wrapper dynamic-method) System.Object.31(intptr,intptr)
04-21 22:37:08.972 E/mono ( 5319): at (wrapper native-to-managed) System.Object.31(intptr,intptr)
04-21 22:37:08.973 E/mono-rt ( 5319): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object.
04-21 22:37:08.973 E/mono-rt ( 5319): at Views.LoginPage+<SigninProcedure>d__2.MoveNext () [0x001d6] in LoginPage.xaml.cs:56
<uses-permission android:name="android.permission.INTERNET" />
下面是我抛出异常的代码:
public async Task<List<User>> FindExistUserAsync(string weburl, string username)
{
string url = weburl + username;
try
{
var response = await client.GetAsync(url);// throw exception after run this line.
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var JsonResult = response.Content.ReadAsStringAsync().Result;
try
{
//Debug.WriteLine(JsonResult);
var contentResp = JsonConvert.DeserializeObject<List<User>>(JsonResult);
return contentResp;
}
catch (Exception ex) { Debug.WriteLine(ex.Message); return null; }
}
}
catch (Exception ex) { Debug.WriteLine(ex.Message); return null; }
return null;
}
答案 0 :(得分:0)
我解决了这个问题。我在这里找到它:Android 8: Cleartext HTTP traffic not permitted。 从Android 9.0(API级别28)开始,默认情况下禁用明文支持。因此,当我尝试连接Web服务时会抛出异常。