title: How do I avoid network on main thread exception if I already use AsyncTask?
body: <p>It seems to me I m not really calling URL,network operations <strong>right from MainActivity</strong>.</p>
  
  <ul>
  <li>I do call url.connect() in another class ProcessData</li>
  <li>I do use doInBackground()   </li>
  <li>I do extend ProcessData class with AsyncTask</li>
  </ul>
  
  <p>but the exception still occurs:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
      private final String LOG_TAG = MainActivity.class.getSimpleName();
      @Override
      protected void onCreate(Bundle savedInstanceState) {            
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ProcessData test = new ProcessData();
          test.doInBackground("mysql") ;
      }
  
  //separate file ProcessData.java
  public class ProcessData extends AsyncTask&lt;String, Void, String&gt; {
  
  public String createValidUrl(String s){
         .....//code in this method works fine
  }
  public String doInBackground(String ...params){
          String validUrl = createValidURL(params[0]);                   
          HttpURLConnection urlConnection = null;
          BufferedReader reader = null;
          URL url = new URL(validUrl);
          urlConnection = (HttpURLConnection) url.openConnection();        
          urlConnection.setRequestMethod("GET");            
          urlConnection.connect();           
  
          InputStream inputStream = urlConnection.getInputStream();
          StringBuffer buffer = new StringBuffer();
  
          reader = new BufferedReader(new InputStreamReader(inputStream));
  
          String line;
              while((line = reader.readLine()) != null) {
  
                  buffer.append(line + "\n");
              }
              urlConnection.disconnect();
              reader.close();
  
              return buffer.toString();
  }
  }
  </code></pre>
  
  <p>The network main thread exception still occurs, i read other SO posts.
  I can wrap my http connection code into new thread, but I want to find exactly what is wrong</p>