6

我試圖連接到需要身份驗證的Rails應用程序服務器。我在桌面應用程序上使用Jakarta HTTP Client for Java,並且它可以100%工作。但是當在Android模擬器上執行完全相同的代碼時,我得到一個IOException。Android手機上的基本HTTP身份驗證到Rails服務器

這裏是代碼,如果有人可以幫我弄清楚爲什麼它會拋出IOException,將不勝感激!

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

你能提供T的相關部分他堆棧跟蹤? – 2010-11-24 17:46:53

回答

2

它不停觸發IOException異常是因爲清單文件並沒有給我使用HttpPost這類任務的應用程序權限到互聯網

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

,和從未有過的原因任何問題:

[...] 
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(); 
[...] 

也許這可以幫助你