6

Estoy intentando conectarme a un Servidor de aplicaciones Rails que requiere autenticación. Estoy usando Jakarta HTTP Client para Java en una aplicación de escritorio y funciona al 100%. Pero cuando se ejecuta exactamente el mismo código en el emulador de Android obtengo una IOException.Autenticación HTTP básica en teléfonos Android a Rails Server

Aquí está el código, y si alguien me puede ayudar a descubrir por qué arroja la IOException que sería muy apreciada.

private boolean login() 
{ 
    String username, password; 

    DefaultHttpClient client; 
    AuthScope scope; 
    Credentials myCredentials; 
    CredentialsProvider provider; 
    HttpEntity entity; 
    String line; 
    BufferedReader reader; 
    InputStream instream; 

    //Declare & Create the HTTP Client 
    client = new DefaultHttpClient(); 

    //Create our AuthScope 
    scope = new AuthScope("10.19.9.33", 3000); 

    username = "admin" 
      password = "pass" 


    //Set Credentials 
    myCredentials = new UsernamePasswordCredentials(username, password); 

    //Set Provider 
    provider = new BasicCredentialsProvider(); 
    provider.setCredentials(scope, myCredentials); 

    //Set Credentials 
    client.setCredentialsProvider(provider); 

    String url = "http://10.19.9.33:3000/users/show/2"; 

    HttpGet get; 

    //Tell where to get 
    get = new HttpGet(url); 

    HttpResponse response; 

    try 
    { 
     response = client.execute(get); 

     entity = response.getEntity(); 

     /* Check to see if it exists */ 
     if(entity != null) 
     { 
      instream = entity.getContent(); 

      try { 

       reader = new BufferedReader(new InputStreamReader(instream)); 

       line = reader.readLine(); 

       if(line.equals("HTTP Basic: Access denied.")) 
        return false; 

       while (line != null) 
       { 
        // do something useful with the response 
        System.out.println(line); 

        line = reader.readLine(); 
       } 

       return true; 

      } 
      catch (IOException ex) 
      { 

       // In case of an IOException the connection will be released 
       // back to the connection manager automatically 
       throw ex; 

      } 
      catch (RuntimeException ex) 
      { 
       // In case of an unexpected exception you may want to abort 
       // the HTTP request in order to shut down the underlying 
       // connection and release it back to the connection manager. 
       get.abort(); 
       throw ex;    
      } 
      finally 
      { 
       // Closing the input stream will trigger connection release 
       instream.close();    
      } 
     } 
    } 
    catch(ClientProtocolException cp_ex) 
    { 

    } 
    catch(IOException io_ex) 
    { 

    } 

    return false; 
} 
+3

Podría dar las partes pertinentes de t él apila rastro? –

+0

La referencia de API para InputStream dice: Esta clase abstracta no proporciona una implementación completamente funcional, por lo que debe ser subclasificada, y al menos el método de lectura() debe ser anulado. Prueba BufferedInputStream. – techiServices

+0

descubrí que la razón por la que no se conectaba a la dirección era que olvidé agregar el permiso de Internet al Manifiesto. Pero ahora el dispositivo parece bloquearse en "response = client.execute (get);" línea... –

Respuesta

2

La razón por la que mantuvo la activación de la IOException era debido a que el archivo de manifiesto no dio los derechos de aplicación a Internet

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
0

que estoy usando HttpPost para este tipo de tarea, y nunca tuvo cualquier problema:

[...] 
DefaultHttpClient client = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(LOGIN_SERVLET_URI); 
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); 
params.add(new BasicNameValuePair("userName", userName)); 
params.add(new BasicNameValuePair("password", password)); 

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); 
httppost.setEntity(p_entity); 
HttpResponse response = client.execute(httppost); 
HttpEntity responseEntity = response.getEntity(); 
[...] 

tal vez esto le ayuda a cabo

Cuestiones relacionadas