简体   繁体   English

Xamarin Android应用程序可在模拟器上运行,使用WebRequest时在真实设备上崩溃

[英]Xamarin Android app works on emulator, crashes on real device when using WebRequest

I am using this example to get some data from a link to an Android device: https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx 我正在使用此示例从指向Android设备的链接中获取一些数据: https : //msdn.microsoft.com/zh-cn/library/456dfw4f(v=vs.110).aspx

The app works fine when using the emulator (Nexus 5 with Android Kit Kat), but when I deploy it (release version) on an actual Android (Samsung S3 mini with Jellybean) the app starts, but crashes after that with "Unfortunately .. has stopped working". 使用仿真器(带有Android Kit Kat的Nexus 5)时,该应用程序运行良好,但是当我在实际的Android(带有Jellybean的Samsung S3 mini)上部署它(发行版)时,该应用程序启动,但此后崩溃,出现“不幸..已停止工作”。 If I don't use WebRequest.Create, all my other components in the app work everywhere. 如果我不使用WebRequest.Create,则应用程序中的所有其他组件都可以在任何地方使用。

WebRequest request = WebRequest.Create(
            "MY LINK HERE"); //with a real link ofcourse

        request.Credentials = CredentialCache.DefaultCredentials;
        WebResponse response = request.GetResponse();


        Stream dataStream = response.GetResponseStream();

        StreamReader reader = new StreamReader(dataStream);

        string responseFromServer = reader.ReadToEnd()

        reader.Close();
        response.Close();

I fixed the ussue. 我解决了问题。 I didn't know that I had to set permission to use internet. 我不知道我必须设置使用互联网的权限。

Project->Project Options->Build->Android Application->Required permission-> Tick the Internet checkbox and press OK. 项目->项目选项->构建-> Android应用程序->所需权限->选中Internet复选框,然后按确定。

Enable Internet permission for App in AndroidManifest.xml file under Properties as follows: 在“属性”下的AndroidManifest.xml文件中为App启用Internet权限,如下所示:

 <uses-permission android:name="android.permission.INTERNET" /> 

or enable through wizard from following steps: 或通过以下步骤通过向导启用:

  • Right click over project and Properties 右键单击项目和属性
  • Under Android Manifest 在Android清单下
  • enable checkbox for Internet under Required Permissions 在所需权限下启用Internet复选框 在此处输入图片说明

Hope this helps 希望这可以帮助

暂无
暂无

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

相关问题 Xamarin 表单,在 Visual Studio Android 模拟器上运行的应用程序但在真实设备上崩溃 - Xamarin forms, App running on Visual Studio Android Emulator But crashes on real device Xamarin应用程序可在模拟器上运行,但不能在设备上运行 - Xamarin app works on emulator but not on device Xamarin.Forms应用程序在设备上的MigrateAsync而非模拟器上崩溃 - Xamarin.Forms app crashes on MigrateAsync on the device but not on the emulator 使用Xamarin的Android模拟器无法打印设备日志 - Device Logs not printing for android emulator using Xamarin Xamarin Android App在ASUS设备上启动后立即崩溃 - Xamarin Android App crashes as soon as it starts on ASUS device xamarin android apk安装在模拟器上,但未安装在设备上 - xamarin android apk installs on emulator but not on device 在模拟器中运行的Xamarin.Android应用中使用Realm会引发RealmFileAccessErrorException - Using Realm in Xamarin.Android app running in emulator throws RealmFileAccessErrorException Xamarin.ios 不适用于真正的 iOS 设备,但它适用于模拟器 - Xamarin.ios does NOT work on real iOS device but it works on Simulator 实现Parse Unity Plugin的应用在Android设备上崩溃,但在编辑器中可以正常运行 - App implementing Parse Unity Plugin crashes on android device but works fine in editor 如何使用 Xamarin for Visual Studio 在真实的 Android 设备上进行调试? - How do I debug on a real Android device using Xamarin for Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM