[英]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: 或通过以下步骤通过向导启用:
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.