Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoDroid HttpWebRequest and WebClient unreliable?

I am having a lot of trouble using Webrequests in MonoDroid and getting timeouts at random. My code works fine then sometimes all requests just timeout and don't work.

I have verified the webservices used in my requests are not the problem.

Here is an example of some code that I may use to request some data from a webservice using MonoDroid:

bool bolOk = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create ("http://www.website.com/service/");
request.Timeout = 20000;
request.Credentials = gv_objCredentials;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse ()) {
    bolOk = response.StatusCode == HttpStatusCode.OK;
}

As you can see it is basic stuff. I use code like the above always on another thread to the UI using ThreadPool.QueueUserWorkItem or TaskFactory.

What I have noticed is that if the requests start timing out from my app and I plug it in to my computer then debug the app from MonoDevelop the requests work without timing out. I am not sure if this means anything. This is similar to testing the webservices from my computer using a browser on the same network as the phone. The webservices always work without any issues.

What is the best way to make Webrequests from MonoDroid?

How can I ensure my requests are always successful and won't timeout if the webservice is operating correctly?

like image 221
startupsmith Avatar asked Jun 20 '12 00:06

startupsmith


2 Answers

I had the Issue on Xamarin 4.2.6 and 4.2.8.

Thanks to Xamarin support, they identified the issue and suggested I targeted my build to armeabi-v7a rather than armeabi in my project properties (some multi-core processor issue described here)

Depending on whether you plan to support multi-core processors or not, you should check our this post and may need to manually edit your .csproj file.

like image 84
Andy Joiner Avatar answered Oct 13 '22 00:10

Andy Joiner


There's a new version of Mono for Android (4.2.5) that fixes a number of bugs with the WebRequest and webRequestStream. You can check the release notes here: http://docs.xamarin.com/android/Releases/Mono_For_Android_4/Mono_for_Android_4.2.5

I suggest downloading the latest bits and check if it works. If not, please file a bug and they will surely fix it in the next version of the product.

like image 44
Willem Meints Avatar answered Oct 13 '22 01:10

Willem Meints