Bonjour,

Je tente de m'authentifier sur un site et le code ci-dessous fonctionne parfaitement sur une application console lambda mais si je tente de le faire fonctionner sous android (1.6 ou 2.1), je reçoit ce message d'erreur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
Caused by: java.lang.StringIndexOutOfBoundsException
at java.lang.String.substring(String.java:1646)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getAuthorizationCredentials(HttpURLConnection.java:1671)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnection.java:1603)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:1551)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1052)
at com.example.testa.MainActivity.onCreate(MainActivity.java:45)
Voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String html = null;
		try{
			final String urlStr = "cible";
			final String domain = ""; 
			final String userName = "username";
			final String password = "password";		
 
			StringBuilder response = new StringBuilder();
 
			Authenticator.setDefault(new Authenticator() {
		        @Override
		        public PasswordAuthentication getPasswordAuthentication() {
		            return new PasswordAuthentication(domain + "\\" + userName, password.toCharArray());
		        }
		    });
 
		    URL urlRequest = new URL(urlStr);
		    HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
		    conn.setDoOutput(true);
		    conn.setDoInput(true);
		    conn.setRequestMethod("GET");
 
		    InputStream stream = conn.getInputStream();
		    BufferedReader in = new BufferedReader(new InputStreamReader(stream));
		    String str = null;
		    while ((str = in.readLine()) != null) {
		        response.append(str);
		    }
		    in.close();		
 
		    html = response.toString();
 
		}
		catch(IOException e){
 
		}
		Toast.makeText(getApplicationContext(), html, Toast.LENGTH_LONG).show();
 
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
 
}
Je ne comprends pas d'où peut venir ce problème.