首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android认证wampserver

Android认证wampserver
EN

Stack Overflow用户
提问于 2015-03-20 16:31:46
回答 2查看 141关注 0票数 1

我试图做一个android登录活动,我创建了一个简单的数据库,其中包含一个表来测试我的登录活动。我的问题是,当我设置一个正确的用户名和密码时,它告诉我用户名和密码是错误的,我在论坛上搜索过,如果有人和我有相似的问题,但我找不到。我真的被困在这个问题上一个多月了。如果有人能帮我扔这个,我会非常感激的。

这是我的Login.java类

代码语言:javascript
复制
public class Login extends Activity implements OnClickListener {
private EditText _myLogin;
private EditText _myPassword;
private String _myContenuLogin;
private String _myContenuPassword;
private boolean _myCheck;
private ProgressDialog _myDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Button lBtnValidate = (Button) findViewById(R.id.authen_btn_valider);
    lBtnValidate.setOnClickListener(this);
    Button lBtnCancel = (Button) findViewById(R.id.authen_btn_annuler);
    lBtnCancel.setOnClickListener(this);

    _myLogin = (EditText) findViewById(R.id.authen_editlogin);
    _myPassword = (EditText) findViewById(R.id.authen_editlmdp);

}


@Override
public void onClick(View v) {

    if (v.getId() == R.id.authen_btn_valider) {
        _myContenuLogin = _myLogin.getText().toString();
        _myContenuPassword = _myPassword.getText().toString();



        MyAsyncTask myTask = new MyAsyncTask();
        myTask.execute(Stat.URL_CHECK);

        if (_myCheck) {
            Toast.makeText(this, "Bienvenue " + _myContenuLogin,
                    Toast.LENGTH_SHORT).show();
            Intent lIntentHome = new Intent(this, Home.class);
            startActivity(lIntentHome);
            this.finish();

        } else {
            Toast.makeText(this,
                    "Nom d'utilisateur ou mot de passe incorrecte",
                    Toast.LENGTH_LONG).show();

        }

    } else if(v.getId() == R.id.authen_btn_annuler){
        this.finish();
    }


}






private class MyAsyncTask extends AsyncTask<String, Void, Boolean>{

    @Override
    protected void onPreExecute() {
        _myDialog = ProgressDialog.show(Login.this, "Vérification", "Attendez SVP...");


    }

    @Override
    //String...params : Array of Strings , on a mis params[0] puisqu'on a une seule chaîne
    protected Boolean doInBackground(String... params) {

        ClientHTTP lClientHTTP=new ClientHTTP(Login.this);

        ArrayList<NameValuePair> list=new ArrayList<NameValuePair>();

        BasicNameValuePair lParamName=new BasicNameValuePair("PARAM_NAME", _myContenuLogin);
        list.add(lParamName);

        BasicNameValuePair lParamPassword=new BasicNameValuePair("PARAM_PASSWORD", _myContenuPassword);
        list.add(lParamPassword);

        _myCheck=lClientHTTP.SendToUrl(params[0], list);

        return _myCheck;
    }
    @Override
    protected void onPostExecute(Boolean result) {
        _myDialog.dismiss();
    }

}

}

--这是我的Stat.java类,包含有用的字符串

代码语言:javascript
复制
public class Stat {
public static final String DB_NAME = "leoni.db";
public static final String TABLE_NAME = "agent_sos";
public static final String COL_ID = "id";
public static final String COL_NOM_PRENOM = "nom_prenom";
public static final String COL_MDP = "mot_de_passe";
public static final String URL_CHECK = "http://127.0.0.1/check.php";

}

这是我的ClientHTTP.java类,它包含连接、获取和发送到url的方法。

代码语言:javascript
复制
public class ClientHTTP {

private Context _mContext;

public ClientHTTP(Context pContext){
    _mContext=pContext;
}
//méthode pour lire le contenu d'un URL
public String readFromUrl(String strURL) {
    URL clientURL = null;
    HttpURLConnection client = null;
    // Créer un buffer , StringBuilder() permet de créer une chaîne de caractères modifiable
    StringBuilder builder = new StringBuilder();
    // Créer un client Http
    try {
        clientURL = new URL(strURL);
        client = (HttpURLConnection) clientURL.openConnection();
    } catch (MalformedURLException e1) {

        e1.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    // Créer un obejet httpget pour utiliser la methode get
    HttpGet httpGet = new HttpGet(strURL);
    try {
        // récuperer la réponse
        HttpResponse response = ((HttpClient) client).execute(httpGet);
        //
        StatusLine statusLine = response.getStatusLine();
        // récuperer le ack
        int statusCode = statusLine.getStatusCode();
        // si ack =200 connexion avec succée
        if (statusCode == 200) {
            //récuperer l'entité
            HttpEntity entity = response.getEntity();
            //récuperer le contenu de l'entité , 
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            // errur du chargement
            Toast.makeText(_mContext, "pas de connexion", Toast.LENGTH_LONG).show();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

public boolean SendToUrl(String strURL,ArrayList<NameValuePair> nameValuePairs) {
    // Créer un buffer
    StringBuilder builder = new StringBuilder();
    // Créer un client Http
    HttpClient client = new DefaultHttpClient();
    // Créer un obejet httppost pour utiliser la methode post
    HttpPost httppost = new HttpPost(strURL);
    try {
        //UrlEncodedFormEntit() : An entity composed of a list of url-encoded pairs
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // récuperer la réponse
        HttpResponse response = client.execute(httppost);

        StatusLine statusLine = response.getStatusLine();
        // récuperer le ack
        int statusCode = statusLine.getStatusCode();
        // si ack =200 connexion avec succée
        if (statusCode == 200) {

            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            // erreur du chargement
            Toast.makeText(_mContext, "pas de connexion", Toast.LENGTH_LONG).show();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Boolean.parseBoolean(builder.toString());
}
}

,最后这是我的check.php文件

代码语言:javascript
复制
<?php
/*mysql_real_escape_string Protège une commande SQL de la présence des caractères spéciaux */
$username = mysql_real_escape_string($_REQUEST['PARAM_NAME']);
$password = mysql_real_escape_string($_REQUEST['PARAM_PASSWORD']);

  mysql_connect("localhost","root","");
  mysql_select_db("leoni");

$result=mysql_query("SELECT nom_prenom,mot_de_passe FROM agent_sos WHERE nom_prenom ='$username' AND mot_de_passe='$password'");
if (mysql_num_rows($result) == 0) {
   echo "false"; 
} else {
   echo "true";
}

?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-27 00:42:18

我的问题是,我使用的是带有wampsever 2.5的mysql旧版本,所以这是一个兼容性问题,我用以下方式修改了我的php脚本:

代码语言:javascript
复制
<?php
$mysqli = new mysqli("localhost", "root", "", "leoni");

/* Vérification de la connexion */
if (mysqli_connect_errno()) {
    printf("Échec de la connexion : %s\n", mysqli_connect_error());
    exit();
}
/* Recuperer les params passées */
$username = $mysqli->real_escape_string($_POST['PARAM_NAME']);
$password = $mysqli->real_escape_string($_POST['PARAM_PASSWORD']);


 $result =  $mysqli->query("SELECT COUNT(*) FROM agent_sos WHERE nom_prenom ='$username' AND mot_de_passe='$password'");
 $row = $result->fetch_row();
 if($row[0] !=0) {
    printf("true");
}else
    printf("false");

/* Fermeture de la connexion */
$mysqli->close();
?>

我用以下内容更改了URL_CHECK192.168.1.4,这是命令ipconfig的结果--我的新问题是,它在模拟器上运行得很完美,但是它不能在设备上工作!那是为什么?

票数 0
EN

Stack Overflow用户

发布于 2015-03-20 17:01:50

改变这一点:

代码语言:javascript
复制
 public class Stat {
  public static final String DB_NAME = "leoni.db";
  public static final String TABLE_NAME = "agent_sos";
  public static final String COL_ID = "id";
  public static final String COL_NOM_PRENOM = "nom_prenom";
  public static final String COL_MDP = "mot_de_passe";
  public static final String URL_CHECK = "http://127.0.0.1/check.php";
 }

在这方面:

代码语言:javascript
复制
 public class Stat {
  public static final String DB_NAME = "leoni.db";
  public static final String TABLE_NAME = "agent_sos";
  public static final String COL_ID = "id";
  public static final String COL_NOM_PRENOM = "nom_prenom";
  public static final String COL_MDP = "mot_de_passe";
  public static final String URL_CHECK = "http://10.0.2.2/check.php"; //If you are using Emulator or your Computer Ip address by using cmd > ipconfig

 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29171478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档