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
| import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.example.dvp.R;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Demarrage extends Activity {
Button seConnecter;
EditText password;
EditText login;
TextView affichage;
public Integer connecter(String login, String password){
String monLogin = "";
String monPass = "";
//String returnString = "0";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(monLogin, login));
nameValuePairs.add(new BasicNameValuePair(monPass, password));
//InputStream is = null;
//String result = "";
HttpResponse response = null;
Integer i = 0;
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.1.34/connexion.php");
//affichage.setText("connextion");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = client.execute(post);
HttpEntity entity = response.getEntity();
String strResponse = EntityUtils.toString(entity);
strResponse = strResponse.trim();
//affichage.setText("la chaine est " + strResponse);
i = Integer.parseInt(strResponse);
//affichage.setText("" + i); //affiche tu text
} catch (Exception e) {
// TODO: handle exception
//Log.e("log_tag", "Erreur de connexion " + e.toString());
Log.e("log_tag", "Erreur de connexion ",e);
}
return i;
}
class LoginTask extends AsyncTask<String,Void,Integer>
{
public Integer doInBackground(String ... params)
{
if (params == null || params.length != 2) throw new IllegalArgumentException("Invalid arguements passed to LoginTask: should be login,password");
String login = params[0];
String password = params[1];
return Integer.valueOf(connecter(login,password));
}
public void onPostExecute(Integer result)
{
if (result != null && result.intValue() == 1) {
Intent ouvrirActivy = new Intent("com.example.dvp.MAINACTIVITY");
Demarrage.this.startActivity(ouvrirActivy); // <= MyActivity étant le nom de la classe englobante
} else {
Demarrage.this.affichage.setText("id incorrect"); // <= ce n'est pas vrai... si le réseau est down ?
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.demarrage);
seConnecter = (Button) findViewById(R.id.bConnect);
password = (EditText) findViewById(R.id.etPass);
login = (EditText) findViewById(R.id.etLog);
affichage = (TextView) findViewById(R.id.tvTest);
seConnecter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//int result;
LoginTask task = new LoginTask();
task.execute(login.getText().toString(), password.getText().toString());
}
});
}
} |
Partager