Ilia Stoilov Ilia Stoilov -4 years ago 113
Android Question

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

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". If I don't use WebRequest.Create, all my other components in the app work everywhere.

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();

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download