App crashes when connecting to the internet

Hey, I wanted to build an App. On startup it should fetch some data from a server. I did this with the following code:

System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.example.com/fetchJSON.php");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
    return reader.ReadToEnd();
}

When running it on WindowsUWP it works fine and I retrieve the data. But when running it on Android it immediatly crashes. I have already added the INTERNET Permission in the Manifests file. And after that I added just for caution every permission there is. But it still crashes. Another "issue" is that I get the following warning:

The $(TargetFrameworkVersion) for Xamarin.Forms.Platform.dll (v8.1) is greater than the $(TargetFrameworkVersion) for your project (v7.0). You need to increase the $(TargetFrameworkVersion) for your project.

Maybe it has something to do with it. The default setting is that Xamarin wants to compile it for Android Oreo, but I want to get it to work on Android 7 and after a while even on Android 5. So I installed all the SDKs,a nd also changed it up in the project settings to use another target framework.

And when I write the response of the page statically into my App everything works fine, just when it's about to make a connection into the internet it crashes. Do you have any idea how I can solve this? And if you need and further Information feel free to ask, I hope I've mentioned the most important things.

Best Answer

  • jgartnerjgartner ✭✭
    Accepted Answer

    Thanks fot the advice but I just got rid of the SSL. Idc if someone hacks me ¯_(ツ)_/¯

Answers

  • Dmitry_UpovalovDmitry_Upovalov RUMember ✭✭

    Where do you execute the code from? It seems to be called right from the main (ui) thread. This way may produce an exception. BTW, can you share the exception message/trace?

    Regarding your second question - you can support Android 5 while targeting Android 8. You can find more details on that here.

  • jgartnerjgartner Member ✭✭

    I executed the code quite at the beginning. But InitializeComponent() has already been executed. I've managed to get an exception message via try catch. It says:

    Error: Trust Failure (one or more errors occured)

    But I don't get why. My website uses valid https, and when I'm opening the very same url in Google Chrome on my phone is also says my certificate is valid. How can I get HTTPS working? I don't really wanna disable it, but I'm glad that HTTP would work ^^

  • Dmitry_UpovalovDmitry_Upovalov RUMember ✭✭
    edited June 2018

    @jgartner, can you check this thread? The issue may be related to your project configuration (HttpClient/SSL/TLS implementation settings).

  • jgartnerjgartner Member ✭✭
    Accepted Answer

    Thanks fot the advice but I just got rid of the SSL. Idc if someone hacks me ¯_(ツ)_/¯

Sign In or Register to comment.