Ver Mensaje Individual
  #2 (permalink)  
Antiguo 22/11/2018, 09:17
__SDP__
 
Fecha de Ingreso: agosto-2010
Ubicación: santiago, CHILE
Mensajes: 564
Antigüedad: 13 años, 8 meses
Puntos: 9
Respuesta: consumir servicio rest

encontre como acceder utilizando un token, de la misma forma envias parametros por post dejo el codigo por si a alguien el sirve, esta clase se llama ClassConnectionV2 porque es otro archivo nomas que utilice para probar

Código Java:
Ver original
  1. package com.example.practice.getpost;
  2.  
  3. import android.os.AsyncTask;
  4.  
  5. import java.io.BufferedInputStream;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.net.HttpURLConnection;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13.  
  14. public class ClassConnectionV2 extends AsyncTask<String, String, String> {
  15.  
  16.  
  17.     @Override
  18.     protected String doInBackground(String... strings) {
  19.  
  20.         HttpURLConnection httpURLConnection = null;
  21.         URL url = null;
  22.         try {
  23.             url = new URL(strings[0]);
  24.         } catch (MalformedURLException e) {
  25.             e.printStackTrace();
  26.         }
  27.  
  28.         try {
  29.             httpURLConnection = (HttpURLConnection) url.openConnection();
  30.  
  31.             String basicAuth = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImZkYjIzYWUyZDNkN2JkMTlmZjBmMTA1NmM5Mzg5NWE3MzQ4N2EzNzdkNDNmYjdhNWQ4Yjg2YTYzYTcwNjY4YWM5MWM2OGNjMzhhNjlhOTY0In0.eyJhdWQiOiIzIiwianRpIjoiZmRiMjNhZTJkM2Q3YmQxOWZmMGYxMDU2YzkzODk1YTczNDg3YTM3N2Q0M2ZiN2E1ZDhiODZhNjNhNzA2NjhhYzkxYzY4Y2MzOGE2OWE5NjQiLCJpYXQiOjE1NDIwMzY2MTEsIm5iZiI6MTU0MjAzNjYxMSwiZXhwIjoxNTczNTcyNjExLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.iQoKJu6bzzqbfXI8YvCsObFaA7MsufHtaRCK3JS3arVq2HWvDP268QxG_NzCACVxnuuvYkHXbtClRzkqYfvcWP_SUhd2jTWUjek5OPueUg7XEu-fXyEbu3LXfK4jL2u2w1w_s8a4MfEJIbEJuxBefMa9u8LkK3HuSBPoLzsxYnhONtXFm_qrAsUKnUQgmYq0cKm-4prO5PzNF1FACgIhfmwdOeuNi66ICajjI9eUxF17nT4Rbn-s4_xt4B81sQ_Mxsxejafz2NS4a1zuNDUzkvpWKb34EhKW5r9Pk4SApYlzfkgarQKqUNKw_UnkZPAOf_oVmboGKi1dtsxX3-Oic16hAckR9s_h5P9A6E1xRa8BoIKd8FZhQtSy4vCcsSbyk3jxua_NR-AnydHRgbM_WujDWLM_eItZrxrembXySKsb4dSCMm6pOqvM6i6JNyIUh5LWrP0M1UrvzfeqWSgF4DFV8GhHbuQ6HOGKlC1lLEEYcPgAiWFRW9d-603Lhc8xp96GA7UrdvbqMtb6dUFHbv5Uhy_Ac0O_-3lzu9X-8Kcqo8sICsTuaiwCQ0274dVTkjzLNYTa5TSsLkp-VnWCjelDq-uE9Y10_JbUhuihZZ-oKnQJZ5bIxJPSZ5sj1BzPCMO031SwAGI7A1eXduD-nLZqSAhkiZQS6FWH6WWCtYE";
  32.             httpURLConnection.setRequestProperty ("Authorization", basicAuth);
  33.             httpURLConnection.setRequestMethod("POST");
  34.  
  35.             httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  36.             httpURLConnection.setUseCaches(false);
  37.             httpURLConnection.setDoInput(true);
  38.             httpURLConnection.setDoOutput(true);
  39.  
  40.             int code = httpURLConnection.getResponseCode();
  41.             String linea = "";
  42.             String line;
  43.  
  44.             if(code == HttpURLConnection.HTTP_OK){
  45.  
  46.                 InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());
  47.  
  48.                 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  49.                 while((line = reader.readLine()) != null){
  50.                     linea += line;
  51.                 }
  52.  
  53.                 return linea;
  54.  
  55.             }
  56.  
  57.             return "nok";
  58.  
  59.         } catch (IOException e) {
  60.             e.printStackTrace();
  61.         }
  62.  
  63.         return null;
  64.     }
  65. }