2012-12-22 78 views
0

當我添加一個授權標題時,android會拋出一個filenotfound異常。如果我刪除添加授權標題導致android拋出一個文件未發現異常

con.setRequestProperty ("Authorization", basicAuth); 

一切都好?

con = (HttpsURLConnection)url.openConnection(); 
      String userpass = userNameEditText.getText() + ":" + passwordEditText.getText(); 

      String basicAuth = "Basic " + Base64.encodeToString(userpass.getBytes(), Base64.NO_WRAP); 

      con.setRequestProperty ("Authorization", basicAuth); 

回答

0

我已經使用了以下,它的工作原理就像一個魅力。

//Base64 basic http authentication 
String textToBeEncoded = mName+":"+mPassword; 
byte[] data = null; 
data = textToBeEncoded.getBytes("UTF-8"); 
encoding = Base64.encodeToString(data,Base64.NO_WRAP); 
Log.d(TAG,"encoded string: "+encoding); 
//-------------------------------- 
HttpGet httpRequest = new HttpGet("//REQUEST URL//"); 
httpRequest.addHeader("Authorization", "Basic "+encoding); 
HttpClient httpclient = new DefaultHttpClient(); 
response = httpclient.execute(httpRequest); 

嘗試使用,而不是這樣..

希望工程。

+0

不是。一樣 – stack

1

我沒有驗證爲什麼它的工作原理,但嘗試做以下

http://developer.android.com/reference/java/net/HttpURLConnection.html使用下面的代碼

Authenticator.setDefault(new Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password.toCharArray()); 

    }); 
} 

你這樣做,沒有必要添加Authorization頭後,因爲這個班會自動爲你添加它。

我有一個應用程序只在某些設備上得到異常,並將代碼從這個代碼更改爲所有設備上的代碼。我希望它會幫助

相關問題