title: Android Async HttpUrlConnection request
body: <p>I have this code for send sign in request:</p>
  
  <pre><code>@Override
  public void onClick(View arg0) {
  
      switch(arg0.getId()) {
  
          case R.id.signInButton: 
              new AsyncSignIn().execute();
              break;
  
          default: 
              break;
      }
  }
  
  private class AsyncSignIn extends AsyncTask&lt;Void, Void, Void&gt; {
  
      protected Void doInBackground(Void...voids) {
          try {
              String email = eField.getText().toString();
              String password = pField.getText().toString();
              if(isOnline(getApplicationContext())) {
                  if(!Requester.signIn(email, password)) {
                      makeToast("Неверный логин или пароль.");
                  }
                  else {
                      _userEmail = email;
                      startMainContentActivity();
                  }
              }
              else {
                  makeToast("Нет доступа к интернету.");
              }
          } catch (IOException e) {
              e.printStackTrace();
          }           
          return null;
      }
  
  }
  </code></pre>
  
  <p>But application crashes with the exception of NetworkOnMainThread: </p>
  
  <pre><code>01-24 20:04:15.248: E/AndroidRuntime(758): android.os.NetworkOnMainThreadException
  </code></pre>
  
  <p>In fact at this string where "handle" is an HttpURLConnection object:</p>
  
  <pre><code> DataOutputStream writer = new DataOutputStream(handle.getOutputStream());
  </code></pre>
  
  <p>I have already tried to use Handler for this, but there was the same result. What's wrong?</p>
  
  <p><strong>Update</strong></p>
  
  <p>This is a function which sends post request to sign in:</p>
  
  <pre><code>protected String sendPost(String _url, String _urlParameters) throws IOException {
      URL url = new URL(_url);
      HttpURLConnection handle = (HttpURLConnection) url.openConnection();
      handle.setRequestMethod("POST");
      handle.setRequestProperty("Host", Host);
      handle.setRequestProperty("Content-Type", POSTContentType);
      handle.setRequestProperty("Content-Length", ""+_urlParameters.getBytes().length);
      handle.setRequestProperty("User-Agent", UserAgent);
      handle.setRequestProperty("Accept", Accept);
      handle.setRequestProperty("Accept-Language", AcceptLang);
      handle.setRequestProperty("Connection", Connection);
      handle.setUseCaches(false);
      handle.setDoOutput(true);
      handle.setDoInput(true);
  
      DataOutputStream writer = new DataOutputStream(handle.getOutputStream()); //App crashed string
      writer.writeBytes(_urlParameters);
      writer.flush();
      writer.close();
      int responseCode = handle.getResponseCode();
  
      if(responseCode == 200) {       
          BufferedReader reader = new BufferedReader(new InputStreamReader(handle.getInputStream())); 
          String inputLine;
          StringBuffer response = new StringBuffer();
  
          while((inputLine = reader.readLine()) != null) {
              response.append(inputLine);
          }
          return response.toString();
      }
      else {
          return null;
      }
  }
  </code></pre>
  
  <p>Permissions from manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"&gt; &lt;/uses-permission&gt;
  </code></pre>
  
  <p><strong>SignIn</strong></p>
  
  <p>There is no any interesting in it.</p>
  
  <pre><code>public boolean signIn(String _email, String _pass) throws IOException {
      String link = signInURL;
      String signInData = "email="+_email+"&amp;password="+_pass;
      String response = sendPost(link, signInData);
      System.out.println(response);
      if(response.compareTo("Logged") == 0) {
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p><strong>makeToast</strong></p>
  
  <p>The same with makeToast - nothing interesting.</p>
  
  <pre><code>protected void makeToast(String mess) {
      Toast t = Toast.makeText(getApplicationContext(), mess, Toast.LENGTH_SHORT);
      t.setGravity(Gravity.CENTER, 0, 0);
      t.show();
  }
  </code></pre>