A voir également:
- Problème avec la connexion à la base de données mysql php
- Gmail connexion - Guide
- Site inaccessible n'autorise pas la connexion - Guide
- Hotmail connexion - Guide
- Formules excel de base - Guide
- D'où peut venir un problème de connexion internet sur un ordinateur ? - Guide
1 réponse
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import static com.example.sony.atb.R.layout.activity_login;
public class login extends AppCompatActivity implements View.OnClickListener {
EditText log, mdp;
String login, pswrd;
Button blogin;
Button benregistrer;
Button bretour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_login);
blogin= (Button) findViewById(R.id.blogin);
blogin.setOnClickListener(this);
benregistrer= (Button) findViewById(R.id.benregistrer);
benregistrer.setOnClickListener(this);
log= (EditText) findViewById(R.id.log);
mdp=(EditText) findViewById(R.id.mdp);
bretour= (Button) findViewById(R.id.bretour);
bretour.setOnClickListener(this);
}
public void userlogin(View v) {
login = log.getText().toString();
pswrd = mdp.getText().toString();
backgroundtask task = new backgroundtask();
task.execute(login, pswrd);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.blogin:
userlogin(v);
break;
case R.id.bretour:
startActivity(new Intent(login.this, Bienvenue.class));
break;
case R.id.benregistrer:
Intent i = new Intent (login.this, MainActivity.class);
startActivity(i);
}
}
private class backgroundtask extends AsyncTask<String,Void,String> {
Context ctx;
AlertDialog alertdialog ;
protected String doInBackground(String... params) {
try {
String login= params[0].toString();
String motdepasse= params[1].toString();
String login_URL ="http://10.0.2.2/appandroid/login.php";
URL url = new URL(login_URL);
String data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(login,"UTF-8");
data+= "&"+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(motdepasse,"UTF-8");
URLConnection httpURLConnection= url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStreamWriter outputStream = new OutputStreamWriter(httpURLConnection.getOutputStream());
outputStream.write(data);
outputStream.flush();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line =null ;
while ((line = bufferedReader.readLine())!=null )
{
sb.append(line);
break;
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertdialog = new AlertDialog.Builder(ctx).create();
alertdialog.setTitle("Login Information.....");
}
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(String result) {
if(result.equalsIgnoreCase("Login success ")){
Intent intent = new Intent(login.this,prives.class);
intent.putExtra(login, log.getText().toString());
startActivity(intent);
}else {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
}
}
</code>
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import static com.example.sony.atb.R.layout.activity_login;
public class login extends AppCompatActivity implements View.OnClickListener {
EditText log, mdp;
String login, pswrd;
Button blogin;
Button benregistrer;
Button bretour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_login);
blogin= (Button) findViewById(R.id.blogin);
blogin.setOnClickListener(this);
benregistrer= (Button) findViewById(R.id.benregistrer);
benregistrer.setOnClickListener(this);
log= (EditText) findViewById(R.id.log);
mdp=(EditText) findViewById(R.id.mdp);
bretour= (Button) findViewById(R.id.bretour);
bretour.setOnClickListener(this);
}
public void userlogin(View v) {
login = log.getText().toString();
pswrd = mdp.getText().toString();
backgroundtask task = new backgroundtask();
task.execute(login, pswrd);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.blogin:
userlogin(v);
break;
case R.id.bretour:
startActivity(new Intent(login.this, Bienvenue.class));
break;
case R.id.benregistrer:
Intent i = new Intent (login.this, MainActivity.class);
startActivity(i);
}
}
private class backgroundtask extends AsyncTask<String,Void,String> {
Context ctx;
AlertDialog alertdialog ;
protected String doInBackground(String... params) {
try {
String login= params[0].toString();
String motdepasse= params[1].toString();
String login_URL ="http://10.0.2.2/appandroid/login.php";
URL url = new URL(login_URL);
String data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(login,"UTF-8");
data+= "&"+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(motdepasse,"UTF-8");
URLConnection httpURLConnection= url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStreamWriter outputStream = new OutputStreamWriter(httpURLConnection.getOutputStream());
outputStream.write(data);
outputStream.flush();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line =null ;
while ((line = bufferedReader.readLine())!=null )
{
sb.append(line);
break;
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertdialog = new AlertDialog.Builder(ctx).create();
alertdialog.setTitle("Login Information.....");
}
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(String result) {
if(result.equalsIgnoreCase("Login success ")){
Intent intent = new Intent(login.this,prives.class);
intent.putExtra(login, log.getText().toString());
startActivity(intent);
}else {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
}
}
</code>