Problème avec la connexion à la base de données mysql php

Fermé
oumayma - 13 mars 2016 à 18:55
 oumayma - 13 mars 2016 à 18:59
Bonsoir
je veux tester un bouton "login", après la saisie d'un login et d'un mot de passe le programme vérifie s'ils sont correctes ou pas, si je fais l'appel à une classe backgroundtask.java ça marche mais je veux qu'elle soit dans login.java alors ce code ça marche pas!!
quelqu'un peut m'aider SVP!!



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



}


}






}

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>
0