Récupération des variables de page PHP dans le client Android
Bonsoir :D
Je veut récupérer le résultat retourné par ma page PHP dans mon programme client Android
code page PHP:
Code:
1 2 3 4 5 6 7 8 9 10 11
|
<?php
$connexion=mysql_connect( "localhost" , "root" , "" );
mysql_select_db("maison");
$requete="SELECT prenom FROM personne";
$resultat=mysql_query($requete)or die(mysql_error());
$personne=mysql_fetch_array($resultat);
?>
<?php
echo $personne['prenom'];
?> |
le code de mon client Android:
Code:
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
|
package com.saturne.externalDB;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener {
// private static final String CLASSTAG = SimpleGet.class.getSimpleName();
private EditText getInput;
private TextView getOutput;
private Button getButton;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.getInput = (EditText) findViewById(R.id.get_input);
getInput.setText("http://192.168.1.4/S1.php");
this.getOutput = (TextView) findViewById(R.id.get_output);
this.getButton = (Button) findViewById(R.id.get_button);
this.getButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getOutput.setText("");
String output = getHttpResponse(getInput.getText().toString());
if (output != null) {
getOutput.setText(output);
}
}
});
};
/**
* Perform an HTTP GET with HttpUrlConnection.
*
* @param location
* @return
*/
private String getHttpResponse(String location) {
String result = null;
URL url = null;
// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " location = " + location);
try {
url = new URL(location);
// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url = " + url);
} catch (MalformedURLException e) {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine;
int lineCount = 0; // limit the lines for the example
while ((lineCount < 10) && ((inputLine = in.readLine()) != null)) {
lineCount++;
// Log.v(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " inputLine = " + inputLine);
result += "\n" + inputLine;
}
in.close();
urlConn.disconnect();
} catch (IOException e) {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
}
} else {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url NULL");
}
return result;
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
} |
le résultat dans l'émulateur est:
http://img43.imageshack.us/img43/5404/phpnd.jpg
Ma question est: ce null dans l'affichage d'où vient t-il? :aie:
y'a t-il un autre moyen pour transmettre les informations ( les variables depuis la page PHP??) :ccool:
:merci:
test login et mot de passe android
Citation:
Envoyé par
Feanorin
Bonjour,
Il te manque quelque chose dans ton code (permission , ...) , et le logcat fournit n'est pas complet . Regardes lorsque tu débogue l'application ce que tu as dans le détails message de ton exception .
bonjour, ça va? :)
je veux tester mon code android (login et password) pour passer a l'interface to mais lorsque je fait la redirection (si login et password passe a l'interface 2 et si aussi erreur passe aussi :( )
svp ou est le problème dans mon code
PHP:
Code:
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
| <?php
include("../inc/config.php");
$login=$_REQUEST['login'];
$password=$_REQUEST['password'];
$verif=$_REQUEST['verif'];
if(isset($_GET['test']))
{
// verifier si le client existe dans la base de données ou non
$sql = "SELECT * FROM `client` WHERE password='$password' and login='$login' ";
$req = mysql_query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
$NumRows=mysql_num_rows($req);
$test='0';
while($data = mysql_fetch_array($req))
{$test='1';}
echo $test;
// fin verifier existance client //
}
if(isset($_GET['nom']))
{
// verifier le nom du client
$sql = "SELECT * FROM `client` WHERE password='$password' and login='$login'";
$req = mysql_query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
$NumRows=mysql_num_rows($req);
$nom='';
while($data = mysql_fetch_array($req))
{$nom=$data['nom'];}
echo $nom;
// fin verifier nom client //
}
if(isset($_GET['prenom']))
{
// verifier le prenom du client
$sql = "SELECT * FROM `client` WHERE password='$password' and login='$login' ";
$req = mysql_query($sql) or die('Erreur SQL!<br>'.$sql.'<br>'.mysql_error());
$NumRows=mysql_num_rows($req);
$prenom='';
while($data = mysql_fetch_array($req))
{$prenom=$data['prenom'];}
echo $prenom;
// fin verifier prenom client //
}
?> |
ANDROID:
Code:
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
| package com.monresto.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
String URL = "http://www.monresto.net/android/users.php?";
String P;
String verif;
// private static final String CLASSTAG = SimpleGet.class.getSimpleName();
private EditText getLogin;
private EditText getPassword;
//private TextView getOutput;
private Button getButton;
public static final int CODE_RETOUR = 0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.getLogin = (EditText) findViewById(R.id.get_login);
getLogin.setText("");
this.getPassword = (EditText) findViewById(R.id.get_password);
getPassword.setText("");
//this.getOutput = (TextView) findViewById(R.id.get_output);
this.getButton = (Button) findViewById(R.id.get_button);
this.getButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//getOutput.setText("");
String logintophp = "&login="+getLogin.getText().toString();
String passwordtophp = "&password="+getPassword.getText().toString();
String P = getHttpResponse(URL+logintophp+passwordtophp);
if(P!=null) {
verif="&test=";
P = getHttpResponse(URL+logintophp+passwordtophp+verif);
Toast.makeText(Main.this, "" +P, Toast.LENGTH_SHORT).show();
verif="&nom=";
String nomclient = getHttpResponse(URL+logintophp+passwordtophp+verif);
Toast.makeText(Main.this, "" +nomclient, Toast.LENGTH_SHORT).show();
verif="&prenom=";
String prenomclient = getHttpResponse(URL+logintophp+passwordtophp+verif);
Toast.makeText(Main.this, "" +prenomclient, Toast.LENGTH_SHORT).show();
}
//Toast.makeText(Main.this, "" +A, Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(Main.this, Acceuil.class);//mContext is the Context variable over here.
//startActivityForResult(intent, CODE_RETOUR);
//startActivity(intent);
//}
//AlertDialog.Builder alt_bld = new AlertDialog.Builder(Main.this);
//alt_bld.setMessage("Salut : " + P)
//.setCancelable(false)
//.setNegativeButton("Entrez", new DialogInterface.OnClickListener() {
//public void onClick(DialogInterface dialog, int id) {
//Toast.makeText(Main.this, "" +P, Toast.LENGTH_SHORT).show();
// Action for 'Validez' Button
//AlertDialog alert = alt_bld.create();
// Title for AlertDialog
//alert.setTitle("monresto.net");
// Icon for AlertDialog
//alert.setIcon(R.drawable.monresto);
//alert.show();
}} );
}
/**
* Perform an HTTP GET with HttpUrlConnection.
*
* @param location
* @return
*/
private String getHttpResponse(String location) {
StringBuffer result = new StringBuffer();
URL url = null;
// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " location = " + location);
try {
url = new URL(location);
// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url = " + url);
} catch (MalformedURLException e) {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
}
if (url != null) {
try {
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine;
int lineCount = 0; // limit the lines for the example
while ((lineCount < 10) && ((inputLine = in.readLine()) != null)) {
lineCount++;
// Log.v(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " inputLine = " + inputLine);
result.append(inputLine);
//result += "\n" + inputLine;
}
in.close();
urlConn.disconnect();
} catch (IOException e) {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
}
} else {
// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url NULL");
}
return result.toString();
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
} |