0

The following function in my Database Manager class is throwing the error: android.os.NetworkOnMainThreadException

This function exists as a public method in a DataBaseManager class that i created which extends SQLiteOpenHelper I am guessing this is because it needs to happen in the background, but im not exactly sure how to set that up.. can someone please help

public byte[] getBlobFromURL(String url) {
        byte[] blobData = null;
        DefaultHttpClient mHttpClient = new DefaultHttpClient();  
        HttpGet mHttpGet = new HttpGet(url);  
        HttpResponse mHttpResponse;
        try {
            mHttpResponse = mHttpClient.execute(mHttpGet);
            if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  

            HttpEntity entity = mHttpResponse.getEntity();
            if ( entity != null) {  

             //ContentValues values = new ContentValues();  
             blobData = EntityUtils.toByteArray(entity);  
             //mContext.getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);  
            } 
        } 
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return blobData;
    }
erik
  • 4,946
  • 13
  • 70
  • 120
  • Covered in many questions: http://stackoverflow.com/questions/9745859/networkonmainthread, http://stackoverflow.com/questions/9729825/networkonmainthread-exception-android, http://stackoverflow.com/questions/8427045/how-to-solve-networkonmainthread-exception, http://stackoverflow.com/questions/9156706/what-is-the-error-new-to-android-programming – kabuko Mar 21 '12 at 19:18
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – laalto Dec 16 '13 at 09:36

4 Answers4

2

It might be better to use an asynctask since it's higher level than Thread

private class GetDataTask extends AsyncTask<Void, Void, String> {

        private String urlToGet = baseUrl;

        public GetDataTask() {

        }                       

        @Override
        protected String doInBackground(Void... params) {

            try {                                       

                result = getUrlContent(this.urlToGet);                                                                              

            } catch (Exception e) {                 

            }

            if (isCancelled()) { 
                return null;
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result) {               
            setData(result);
        }
    }
2

This has already been pretty thoroughly covered, but read the doc on Painless Threading. You want to do your network calls in another thread, probably by putting it in the doBackground method of an AsyncTask.

kabuko
  • 36,028
  • 10
  • 80
  • 93
2

Use AsyncTask. Or use a Thread with an inline anonymous Runnable as an argument. The latter is easier, IMHO.

new Thread(new Runnable(){
    public void run()
    {
        DefaultHttpClient mHttpClient = new DefaultHttpClient();   
        //And so forth...
        //...
    }
}).start();

To make outer variables (like url) visible to the anonymous class, mark them as final.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
1

you are getting this exception because you are running blocking code in the UI thread. You have to run it in background either using thread or AsyncTask. Check this android guide

Blackbelt
  • 156,034
  • 29
  • 297
  • 305