添加授权标头会导致android引发filenotfound异常

添加授权标头会导致android引发filenotfound异常,android,Android,当我添加授权头时,android抛出一个filenotfound异常。如果我移除 con.setRequestProperty ("Authorization", basicAuth); 一切都好吗 con = (HttpsURLConnection)url.openConnection(); String userpass = userNameEditText.getText() + ":" + passwordEditText.getText();

当我添加授权头时,android抛出一个filenotfound异常。如果我移除

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);

我用了下面的方法,效果很好

//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);
试着用这种方法代替


希望它能起作用。

我没有验证它为什么能起作用,但请尝试执行以下操作

从中使用以下代码

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

   });
 }
执行此操作后,无需添加授权标头,因为此类将自动为您添加授权标头

我有一个只在某些设备上出现异常的应用程序,将代码从您的代码更改为此代码可以在所有设备上为我修复它

我希望这会有帮助