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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| package com.android.authentification;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class AuthentificationActivity extends Activity {
private StringBuffer stringBuffer = new StringBuffer("");
private BufferedReader bufferedReader = null ;
private static final String url = "http://192.168.5.29:8080/testWeb/servlet.AuthentificationServlet";
public static ProgressDialog progressDialog;
private EditText UserEditText;
private EditText PassEditText;
private static final String LOG_TAG = "Log : ";
private final String mimeType = "text/html";
private final String encoding = "utf-8";
private String pageWeb;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialisation de la progress bar
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Attendez...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
webView = (WebView) findViewById(R.id.WebView);
// Récupération des éléments de la vue définis dans le xml
UserEditText = (EditText)findViewById(R.id.username);
PassEditText = (EditText)findViewById(R.id.password);
Button button =(Button)findViewById(R.id.okbutton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// new Thread(){
// public void run(){
int usersize = UserEditText.getText().length();
int passsize = PassEditText.getText().length();
//si les deux champs sont remplis
if(usersize>0 && passsize>0)
{
progressDialog.show();
String login = UserEditText.getText().toString();
String pass = PassEditText.getText().toString();
//on appelle la fonction dologin qui va communiquer av le serveur
try {
pageWeb = doLogin(login,pass);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL("fake://not/needed", pageWeb, mimeType, encoding, "");
webView.getSettings().setJavaScriptEnabled(true);
}
else
createDialog("Erreur","Entrez votre login et mot de passe");
}
//}.start();
}//}
);
//le bouton cancel pour sortir de l'application
Button button1 =(Button)findViewById(R.id.cancelbutton);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
quit(false,null);
}
});
}//create
//---------------------------------------------------------------------------------------------------------------
private void quit(boolean b, Intent i) {
//on envoie un résultat qui va permettre de quitter l'application
setResult((b)? Activity.RESULT_OK : Activity.RESULT_CANCELED, i);
finish();
}
//---------------------------------------------------------------------------------------------------------------
private void createDialog(String title, String text) {
AlertDialog ad = new AlertDialog.Builder(this)
.setPositiveButton("OK", null).setTitle(title).setMessage(text)
.create();
ad.show();
}
//---------------------------------------------------------------------------------------------------------------
private String doLogin(final String login, final String pass)throws ClientProtocolException, IOException {
Thread t = new Thread(){
public void run(){
Looper.prepare();
try{
//on se connecte au serveur afin d'envoyer le login et le passwd
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
//la requete qui envoie les paramètres
HttpPost post = new HttpPost(url);
//on crée la liste qui contient les paramètres
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//on rajoute les paramètres à la liste
nvps.add(new BasicNameValuePair("username", login));
nvps.add(new BasicNameValuePair("password", pass));
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//joindre les paramètre à la requete
post.setEntity(new UrlEncodedFormEntity(nvps));
//Execution du client HTTP avec le HttpPost
HttpResponse response = client.execute(post);
//On récupère la réponse dans un InputStream
InputStream is = response.getEntity().getContent();
Log.d("myapp", "response " + response.getEntity().getContent());
//On crée un bufferedReader pour pouvoir stocker le résultat dans un string
bufferedReader = new BufferedReader(new InputStreamReader(is));
//On lit ligne à ligne le bufferedReader pour le stocker dans le stringBuffer
String ligneCodeHTML = bufferedReader.readLine();
while (ligneCodeHTML != null){
stringBuffer.append(ligneCodeHTML);
stringBuffer.append("\n");
ligneCodeHTML = bufferedReader.readLine();
}
}
catch (Exception e)
{
progressDialog.dismiss();
createDialog("Erreur", "impossible d'établir la connexion");
}
Looper.loop();
}
};
t.start();
return stringBuffer.toString();
}
}//classe |
Partager