title: How to Async Task Network Operation in Service in Android?
body: <p>This is my Service which does network operation. But it is throwing NetworkonMainThreadException which i understand android upper version doesn't allows network operation under main thread. Now i want to use Async Task for this purpose. I am not sure which are all code i need to add under Async Task from Service Class to actually make the code complete. Below is my Service Code : </p>
  
  <pre><code>public class NewsTickerDataService extends Service {
  
   @Override
      public void onStart(Intent aIntent, int aStartId) {
          super.onStart(aIntent, aStartId);   
          RemoteViews _views = buildUpdatedViews(this);   
          ComponentName _widget = 
              new ComponentName(this, NewsTicker.class);
          AppWidgetManager _manager = 
              AppWidgetManager.getInstance(this); 
          _manager.updateAppWidget(_widget, _views);
      }
  
      @Override
      public IBinder onBind(Intent aParamIntent) {
          // not supporting binding   
          return null;
      }
  
      private RemoteViews buildUpdatedViews(Context aContext) {
          List&lt;Story&gt; _stories = getStories();
          RemoteViews _result = new RemoteViews(
              aContext.getPackageName(), 
              R.layout.activity_main
          );
  
          if (_stories.isEmpty()) {
              _result.setTextViewText(R.id.title, 
                  "Sadly there's nothing to read today.");
          } else {
              _result.setTextViewText(
                  R.id.title, _stories.get(0).getTitle());
          }
          return _result;
      }
  
      private List&lt;Story&gt; getStories() {
          try {
              URL _url = new URL("http://search.twitter.com" +
                  "/search.atom?q=%23uml&amp;" + 
                  "result_type=mixed&amp;count=5"
              );
              InputStream _in = _url.openStream();
              return parse(new InputSource(_in));
          } catch (Exception anExc) {
              Log.e("NewsTicker", anExc.getMessage(), anExc);
              return new ArrayList&lt;Story&gt;();
          }
      }
  
      private List&lt;Story&gt; parse(InputSource aSource)
      throws Exception {
          SAXParserFactory _f = SAXParserFactory.newInstance();
          SAXParser _p = _f.newSAXParser();
          XMLReader _r = _p.getXMLReader();
  
          AbstractParser _h = AbstractParser.newAtomParser();
          _r.setContentHandler(_h);   
          _r.parse(aSource);
  
          return _h.getStories();
      }
  
  }
  </code></pre>
  
  <p>Async Task Code : </p>
  
  <pre><code>public class YourAsyncTask extends AsyncTask&lt;String, Void, String&gt;{
  
          @Override
          protected String doInBackground(String... params) {
              // your load work
              //return myString;
          }
  
          @Override
          protected void onPostExecute(String result) {
  
          }
  
      }
  </code></pre>
  
  <p>Can someone please help me to integrate Async Task into same code. Thanks</p>