Descargar el PDF Descargar el PDF

Las solicitudes HTTP post se utilizan en Java para solicitar que un servidor web específico reciba y almacene datos ingresados a través de un formulario de petición. Los datos se envían y almacenan en pares con formato nombre-valor . Por ejemplo, los pares pueden ser email-tu dirección de correo; usuario-tu nombre de usuario; contraseña-tu contraseña.

Parte 1
Parte 1 de 3:

Crear un bloque try, HTTPClient y HTTPPost

Descargar el PDF
  1. 1
    Crea un bloque try . En java, la sentencia try sirve para identificar las excepciones que podrían causar problemas en tu programa a nivel de ejecución. [1]
     try 
     { 
     aquí 
     van 
     las 
     sentecias 
     que 
     pueden 
     provocar 
     excepciones 
     } 
    
  2. Los clientes HTTP ejecutan los métodos HTTP: HTTP request (solicitud) y HTTP response (respuesta). [2]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     } 
    
  3. 3
    Genera un HTTP post. Los HTTP post se utilizan para solicitarle a un servidor específico que acepte y almacene información enviada, a través de un mensaje de petición, como por ejemplo, un formulario. [3]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     } 
    
    Anuncio
Parte 2
Parte 2 de 3:

Crear una lista con formato List<NameValuePair> y una lista de arreglos ArrayList<NameValuePair>

Descargar el PDF
  1. 1
    Crea una lista con el formato List<NameValuePair>. Los pares nombre-valor (<NameValuePair>) se utilizan para configurar parámetros y almacenar datos de solicitudes HTTP. [4]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     List 
     < 
     NameValuePair 
     > 
     params 
     } 
    
  2. 2
    Crea un ArrayList<NameValuePair>. Las funciones de List<NameValuePair> se ejecutan a través de un ArrayList<NameValuePair>. Si bien los arreglos estándares son estáticos, los ArrayList son dinámicos. [5]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     List 
     < 
     NameValuePair 
     > 
     params 
     = 
     new 
     ArrayList 
     < 
     NameValuePair 
     >(); 
     } 
    
  3. Un <NameValuePair> conecta un "nombre" con un "valor". Los datos luego se almacenan de a pares. [6]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     List 
     < 
     NameValuePair 
     > 
     params 
     = 
     new 
     ArrayList 
     < 
     NameValuePair 
     >(); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "name" 
     , 
     "value" 
     )); 
     } 
    
  4. 4
    Inserta valores en la lista. Entre los posibles nombres y valores, puedes usar: [7]
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "usuario" 
     , 
     "tunombredeusuario" 
     )); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "email" 
     , 
     "tudireccióndecorreo" 
     )); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "clave_de_acceso" 
     , 
     "tucontraseña" 
     )); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "tipo_de_cuenta" 
     , 
     "tutipodecuenta" 
     )); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "fuente" 
     , 
     "tufuente" 
     )); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "servicio" 
     , 
     "tuservicio" 
     )); 
    
    Anuncio
Parte 3
Parte 3 de 3:

Ejecutar la solicitud HTTP post y atrapar las excepciones

Descargar el PDF
  1. 1
    Incluye un comando httppost.setEntity . El comando httppost.setEntity te permite recuperar los datos almacenados en las listas List<NameValuePair> y ArrayList<NameValuePair>. [8]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     List 
     < 
     NameValuePair 
     > 
     params 
     = 
     new 
     ArrayList 
     < 
     NameValuePair 
     >(); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "nombre" 
     , 
     "valor" 
     )); 
     httppostset 
     . 
     entity 
     ent 
     = 
     new 
     UrlEncodedFormEntity 
     ( 
     params 
     , 
     HTTP 
     . 
     UTF_8 
     ); 
     HttpEntity 
     resEntityGet 
     = 
     responseGet 
     . 
     getEntity 
     (); 
     } 
    
  2. 2
    Crea una respuesta HTTP y una entidad HTTP. Una vez que el servidor haya recibido y descifrado la solicitud HTTP post, te informará los resultados. [9]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     List 
     < 
     NameValuePair 
     > 
     params 
     = 
     new 
     ArrayList 
     < 
     NameValuePair 
     >(); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "nombre" 
     , 
     "valor" 
     )); 
     httppostset 
     . 
     entity 
     ent 
     = 
     new 
     UrlEncodedFormEntity 
     ( 
     params 
     , 
     HTTP 
     . 
     UTF_8 
     ); 
     httpResponse 
     response 
     = 
     client 
     . 
     execute 
     ( 
     post 
     ); 
     httpEntity 
     resEntityGet 
     = 
     responseGet 
     . 
     getEntity 
     (); 
     } 
    
  3. 3
    Atrapa las excepciones. Las sentencias try identifican o capturan las excepciones que pueden impedir el normal funcionamiento del programa. Las sentencias catch le indican al servidor qué hacer cuando se produzcan esas excepciones. [10]
     try 
     { 
     HttpClient 
     client 
     = 
     new 
     DefaultHttpClient 
     (); 
     HttpPost 
     post 
     = 
     new 
     HttpPost 
     ( 
     http 
     : 
     //IngresaAquíLaDirecciónDelPost); 
     List 
     < 
     NameValuePair 
     > 
     params 
     = 
     new 
     ArrayList 
     < 
     NameValuePair 
     >(); 
     nameValuePairs 
     . 
     add 
     ( 
     new 
     BasicNameValuePair 
     ( 
     "nombre" 
     , 
     "valor" 
     )); 
     httppostset 
     . 
     entity 
     ent 
     = 
     new 
     UrlEncodedFormEntity 
     ( 
     params 
     , 
     HTTP 
     . 
     UTF_8 
     ); 
     httpResponse 
     response 
     = 
     client 
     . 
     execute 
     ( 
     post 
     ); 
     httpEntity 
     resEntityGet 
     = 
     responseGet 
     . 
     getEntity 
     (); 
     } 
     catch 
     ( 
     IOException 
     e 
     ) 
     { 
     e 
     . 
     printStackTrace 
     (); 
     } 
    
    Anuncio

Cosas que necesitarás

  • IDE de Java
  • herramientas de desarrollo Android (SDK, emulador, etc.)
  • teléfono de desarrollo Android o dispositivo que funcione con Android (opcional para hacer pruebas)
  • conexión a Internet (para hacer pruebas)

Referencias

  1. http://www.dummies.com/how-to/content/try-statements-in-java.html , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
  2. https://hc.apache.org/httpcomponents-client-ga/tutorial/html/preface.html , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
  3. https://hc.apache.org/httpcomponents-client-ga/tutorial/html/preface.html , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html , https://en.wikipedia.org/wiki/POST_(HTTP)
  4. http://stackoverflow.com/questions/17607589/what-is-the-use-of-listnamevaluepair-or-arraylistnamevaluepair , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , http://www.vogella.com/tutorials/ApacheHttpClient/article.html , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , http://stackoverflow.com/questions/4792356/can-anyone-explain-me-this-code
  5. http://stackoverflow.com/questions/17607589/what-is-the-use-of-listnamevaluepair-or-arraylistnamevaluepair , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , http://www.vogella.com/tutorials/ApacheHttpClient/article.html , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , http://stackoverflow.com/questions/4792356/can-anyone-explain-me-this-code
  6. http://stackoverflow.com/questions/4792356/can-anyone-explain-me-this-code , https://docs.oracle.com/javase/7/docs/api/org/omg/CORBA/NameValuePair.html
  7. http://www.vogella.com/tutorials/ApacheHttpClient/article.html , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
  8. http://stackoverflow.com/questions/4792356/can-anyone-explain-me-this-code , http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/ , http://www.vogella.com/tutorials/ApacheHttpClient/article.html
  9. http://www.vogella.com/tutorials/ApacheHttpClient/article.html , http://www.androidhive.info/2011/10/android-making-http-requests/ , http://stackoverflow.com/questions/4792356/can-anyone-explain-me-this-code

Acerca de este wikiHow

Esta página ha recibido 45 899 visitas.

¿Te ayudó este artículo?

Anuncio