title: Httpclient deprecated
body: <p>I'm developing an app using <code>HTTPclient</code> for datatransfer. Since <code>HTTPClient</code> is deprecated, I want to port the network part to <code>URLConnection</code>.</p>
  
  <p>ConectionHttpClient.java</p>
  
  <pre><code>package conexao;
  
  import java.util.ArrayList;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URI;
  
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.conn.params.ConnManagerParams;
  import org.apache.http.params.HttpConnectionParams;
  import org.apache.http.params.HttpParams;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  
  public class ConexaoHttpClient {
      public static final int HTTP_TIMEOUT = 30 * 1000;
      private static HttpClient httpClient;
      private static HttpClient getHttpClient(){
          if (httpClient == null){
              httpClient = new DefaultHttpClient();
              final HttpParams httpParams = httpClient.getParams();
              HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_TIMEOUT);
              HttpConnectionParams.setSoTimeout(httpParams, HTTP_TIMEOUT);
              ConnManagerParams.setTimeout(httpParams, HTTP_TIMEOUT);
          }return httpClient;
  
      }
  
  public static String executaHttpPost(String url, ArrayList&lt;NameValuePair&gt; parametrosPost) throws Exception{
      BufferedReader bufferedReader = null;
      try{
          HttpClient client = getHttpClient();
          HttpPost httpPost = new HttpPost();
          UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parametrosPost);
          httpPost.setEntity(formEntity);
          HttpResponse httpResponse = client.execute(httpPost);
          bufferedReader = new BufferedReader(new InputStreamReader(httpPost.getEntity().getContent()));
          StringBuffer stringBuffer = new StringBuffer(" ");
          String line = " ";
          String LS = System.getProperty("line.separator");
          while ((line = bufferedReader.readLine()) != null){
              stringBuffer.append(line + LS); 
          }bufferedReader.close();
  
  
      String resultado = stringBuffer.toString();
      return resultado;
  }finally{
      if (bufferedReader != null){
          try{
              bufferedReader.close();
          }catch(IOException e){
              e.printStackTrace();
          }
      }
  }
  
  }
  }
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.app.arts;
  
  import java.util.ArrayList;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  
  import conexao.ConexaoHttpClient;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  public cla`enter code here`ss MainActivity extends Activity {
  
      EditText editEmail, editSenha;
      Button btnEntrar, btnEsqueciSenha, btnCadastrar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
      editEmail = (EditText)findViewById(R.id.editEmail);
      editSenha = (EditText)findViewById(R.id.editSenha);
      btnEntrar = (Button)findViewById(R.id.btnEntrar);
      btnEsqueciSenha = (Button)findViewById(R.id.btnEsqueciSenha);
      btnCadastrar = (Button)findViewById(R.id.btnCadastrar);
  
      btnEntrar.setOnClickListener(new View.OnClickListener() {
  
  
          public void onClick(View v){
  
          String urlPost="http://192.168.25.5/arts/admin/login.php";
          ArrayList&lt;NameValuePair&gt; parametrosPost = new ArrayList&lt;NameValuePair&gt;();
          parametrosPost.add(new BasicNameValuePair("email", editEmail.getText().toString()));
          parametrosPost.add(new BasicNameValuePair("senha", editSenha.getText().toString()));
          String respostaRetornada = null;
          try{
           respostaRetornada = ConexaoHttpClient.executaHttpPost(urlPost, parametrosPost);
           String resposta = respostaRetornada.toString();
           resposta = resposta.replaceAll("//s+", "");
           if (resposta.equals("1"))
             mensagemExibir("Login", "Usuario Valido");
           else
             mensagemExibir("Login", "Usuario Invalido");  
          }catch(Exception erro){
            Toast.makeText(MainActivity.this, "Erro: " +erro, Toast.LENGTH_LONG);
           }  
         }    
           public void mensagemExibir(String titulo, String texto){
        AlertDialog.Builder mensagem = new AlertDialog.Builder(MainActivity.this);
        mensagem.setTitle(titulo);
        mensagem.setMessage(texto);
        mensagem.setNeutralButton("OK", null);
        mensagem.show();
  
  
       }
  
  
      });
  }
  }
  </code></pre>