java - Httpclient deprecated -


i'm developing app using httpclient datatransfer. since httpclient deprecated, want port network part urlconnection.

conectionhttpclient.java

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<namevaluepair> 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();         }     } }  } } 

mainactivity.java

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<namevaluepair> parametrospost = new arraylist<namevaluepair>();         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();        }       }); } } 

this solution have applied problem httpclient deprecated in version of android 22

metod get

 public static string getcontenxtweb(string urls) {     string pagina = "", devuelve = "";     url url;     try {         url = new url(urls);         httpurlconnection conexion = (httpurlconnection) url                 .openconnection();         conexion.setrequestproperty("user-agent",                 "mozilla/4.0 (compatible; msie 7.0; windows nt 5.1)");         if (conexion.getresponsecode() == httpurlconnection.http_ok) {             bufferedreader reader = new bufferedreader(                     new inputstreamreader(conexion.getinputstream()));             string linea = reader.readline();             while (linea != null) {                 pagina += linea;                 linea = reader.readline();             }             reader.close();              devuelve = pagina;         } else {             conexion.disconnect();             return null;         }         conexion.disconnect();         return devuelve;     } catch (exception ex) {         return devuelve;     } } 

metodo post

 public static final string user_agent = "mozilla/5.0";    public static string sendpost(string _url,map<string,string> parameter)  {     stringbuilder params=new stringbuilder("");     string result="";     try {     for(string s:parameter.keyset()){         params.append("&"+s+"=");              params.append(urlencoder.encode(parameter.get(s),"utf-8"));     }       string url =_url;     url obj = new url(_url);     httpsurlconnection con = (httpsurlconnection) obj.openconnection();      con.setrequestmethod("post");     con.setrequestproperty("user-agent", user_agent);     con.setrequestproperty("accept-language", "utf-8");      con.setdooutput(true);     outputstreamwriter outputstreamwriter = new outputstreamwriter(con.getoutputstream());     outputstreamwriter.write(params.tostring());     outputstreamwriter.flush();      int responsecode = con.getresponsecode();     system.out.println("\nsending 'post' request url : " + url);     system.out.println("post parameters : " + params);     system.out.println("response code : " + responsecode);      bufferedreader in = new bufferedreader(new inputstreamreader(con.getinputstream()));     string inputline;     stringbuffer response = new stringbuffer();      while ((inputline = in.readline()) != null) {         response.append(inputline + "\n");     }     in.close();          result = response.tostring();     } catch (unsupportedencodingexception e) {         e.printstacktrace();     } catch (malformedurlexception e) {         e.printstacktrace();     } catch (protocolexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     }catch (exception e) {         e.printstacktrace();     }finally {     return  result;     }  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -