---
title: "We are writing an android app which loads some data from remote database using a URL. While doing so we   encountered network on main thread exception. What did we do wrong and how can we handle it?"  
description: "We are writing an android app which loads some data from remote database using a URL. While doing so we   encountered network on main thread exception. What did we do wrong and how can we handle it?"  
author: "Yakshit Agarwal"  
published: 2018-01-22  
canonical: https://answers.mindstick.com/qa/33375/we-are-writing-an-android-app-which-loads-some-data-from-remote-database-using-a-url-while-doing-so-we-encountered-network-on-main-thread-exception-what-did-we-do-wrong-and-how-can-we-handle-it  
category: "technology"  
tags: ["android"]  
reading_time: 1 minute  

---

# We are writing an android app which loads some data from remote database using a URL. While doing so we   encountered network on main thread exception. What did we do wrong and how can we handle it?

We are [writing](https://www.mindstick.com/articles/23547/5-golden-rules-for-writing-excellent-college-papers) an [android](https://www.mindstick.com/articles/1672/android-internals) app which loads some [data](https://yourviews.mindstick.com/view/84027/what-does-google-do-with-your-data) from [remote database](https://www.mindstick.com/articles/198522/it-metrics-for-a-new-company-how-can-a-remote-dba-help) using a URL. While doing so we

encountered [network](https://www.mindstick.com/articles/342247/choosing-the-right-business-network-solution-for-growth) on main [thread exception](https://www.mindstick.com/forum/33892/how-to-handle-cross-thread-exception-in-winforms). What did we do [wrong](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords) and how can we [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) it?

## Answers

### Answer by Prateek sharma

This exception is thrown since the user tries to connect to the [database](https://www.mindstick.com/articles/100/windows-service-to-update-record-from-one-database-to-another-after-certain-interval-of-time) on the **main thread**. since network connections are to be handled in the **background** because they take heavy load which causes the main thread to work more than usual and raises an exception. You need to do these network connections in the separate thread that is background thread. To do this you can use **AyncTask** class, **SyncAdapter** or other libraries such as **okHttp**, etc.


---

Original Source: https://answers.mindstick.com/qa/33375/we-are-writing-an-android-app-which-loads-some-data-from-remote-database-using-a-url-while-doing-so-we-encountered-network-on-main-thread-exception-what-did-we-do-wrong-and-how-can-we-handle-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
