Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a background thread
    text
    copied!<p>I'm currently working toward a mobile android application. The main thing that this app will have trouble with for load times is a Webservice json string that at this current stage is taking too long to load and sometimes causing the app to force close (stalling for too long).</p> <p>Splash -> MainActivity -> HomeActivity This is how our application starts.</p> <p>First we display a Splash, and behind that we run the MainActivity, which consists of the following code:</p> <pre><code>public class HomeActivity : Activity { NewsObject[] news; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); var request = HttpWebRequest.Create(string.Format(@"http://rapstation.com/webservice.php")); request.ContentType = "application/json"; request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var content = reader.ReadToEnd(); if(string.IsNullOrWhiteSpace(content)) { Console.Out.WriteLine("Response contained empty body..."); Toast toast = Toast.MakeText (this, "No Connection to server, Application will now close", ToastLength.Short); toast.Show (); } else { news = JsonConvert.DeserializeObject&lt;NewsObject[]&gt;(content); } } Console.Out.WriteLine ("Now: \r\n {0}", news[0].title); } var list = FindViewById&lt;ListView&gt; (Resource.Id.list); list.Adapter = new HomeScreenAdapter (this, news); list.ItemClick += OnListItemClick; var Listen = FindViewById&lt;Button&gt; (Resource.Id.btnListen); var Shows = FindViewById&lt;Button&gt; (Resource.Id.btnShows); Listen.Click += (sender, e) =&gt; { var second = new Intent (this, typeof(RadioActivity)); StartActivity (second); }; Shows.Click += (sender, e) =&gt; { var second = new Intent (this, typeof(ShowsActivity)); StartActivity (second); }; } protected void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e) { var listView = sender as ListView; var t = news[e.Position]; var second = new Intent (this, typeof(NewsActivity)); second.PutExtra ("newsTitle", t.title); second.PutExtra ("newsBody", t.body); second.PutExtra ("newsImage", t.image); second.PutExtra ("newsCaption", t.caption); StartActivity (second); Console.WriteLine("Clicked on " + t.title); } } </code></pre> <p>The problem I am running into is the app will stick on the Splash page and the Application output will tell me that I am running too much on the Main thread. </p> <p>What is a way to separate the download request to work in the background?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload