I am trying to download the string content from an endpoint in question using xamarin and http requests. The endpoint I'm supporting with python flask, as well as the content of the page, which is similar to this:
[{"name":"test1","timestamp":12312,"type":"ultrasonic","value":65535}, {"name":"test2","timestamp":3123123,"type":"ultrasonic","value":65535}...]
When there are not many lines in the content of the page, I can access the content and save it to a file inside the android device. When the content is very long, with many lines, the application simply closes.
My code is as follows:
public async Task GetNews() { List<string> valores = new List<string>(); HttpResponseMessage httpResponseMessage = await client.GetAsync(get_url).ConfigureAwait(false); HttpResponseMessage response = httpResponseMessage; response.EnsureSuccessStatusCode(); var responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false); string nome = DateTime.Now.ToString("HH:mm:ss"); //creating the subsequent file string filename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Sensor File - " + nome + ".json"); //inserting the content of the GET into the generated text file System.IO.File.WriteAllText(filename, responseData); } public async void Download_Clicked(object sender, EventArgs e) { try { await GetNews(); //informing in the first label the directory of the saved file lb.Text = "File saved successfully!"; } catch { lb.Text = "Connection not possible, enter the correct URL."; } }
I did add .ConfigureAwait(false) to each await line, but it didn't work... What am I doing wrong?
Thank you!
Answers
Presumably the app is throwing an exception. Post the details (type and stack trace) of the exception here.
How long (in bytes) is very long?
Hello!
The error is:
Java.Net.ProtocolException: 'unexpected end of stream'
And is on the line
HttpResponseMessage httpResponseMessage = await client.GetAsync(get_url).ConfigureAwait(false);
If the endpoint have less than 30kb of text, it's ok, the app runs like a charm.
However, more than 30kb, like 2mb, I stuck in
HttpResponseMessage httpResponseMessage = await client.GetAsync(get_url).ConfigureAwait(false);
and the app closes.Have you seen the answers by @LandLu at https://forums.xamarin.com/discussion/176254/java-net-protocolexception-unexpected-end-of-stream-i-got-this-error - hopefully they will help.
If that doesn't help, Googling "Java.Net.ProtocolException unexpected end of stream" does come up with quite a few matches that might be worth checking.
Sorry to not be more help, but it's not one I've experienced.