Tengo una problema un poco raro con Asyctask
y php
.
Si en php
pongo echo "1-1"; todo funciona correcto. Pero si pongo php
con mysql
y la respuesta es 1-1 no funciona. He puesto un Task
en android
y veo lo que lea de php
, la respuesta en android
es 1-1 pero con mucho espacio antes, algo así " 1-1".
Como puedo solucionar esta problema? E intentado con sacar del string
el espacio,pero no a funcionado tampoco.Y en la web php mysal
esta claro que la respuesta es 1-1 sin nada de espacio.
Android JaVa
private class AsyncRetrieve extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(MainPageActivity.this);
HttpURLConnection conn;
URL url = null;
//this method will interact with UI, here display loading message
@Override
protected void onPreExecute() {
super.onPreExecute();
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
// This method does not interact with UI, You need to pass result to onPostExecute to display
@Override
protected String doInBackground(String... params) {
try {
Intent intent = getIntent();
final String str12 = intent.getStringExtra("message");
// Enter URL address where your php file resides
url = new URL("http://www.ssss.ss/api/puente.php?dni=" + str12);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
// this method will interact with UI, display result sent from doInBackground method
@Override
protected void onPostExecute(String result) {
pdLoading.dismiss();
if(result.equals("0-0")) {
// Toast.makeText(MainPageActivity.this, "nada elegido", Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Nada elegido");
builder.setMessage("No has elegido ningun puente hasta ahora! Queres elegir ahora? ");
// add a button
builder.setPositiveButton("Si",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
texts = (TextView)findViewById(R.id.texts);
Intent intent = getIntent();
final String str = intent.getStringExtra("message");
Intent myIntent = new Intent(getApplicationContext(),Puente.class);
myIntent.putExtra("dni", str);
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("0-1")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Has elegido solo las 50%");
builder.setMessage("Has elegido 23,26,27 Diciembre! Queres elegir ahora los de 33%? ");
// add a button
builder.setPositiveButton("Si",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
texts = (TextView)findViewById(R.id.texts);
Intent intent = getIntent();
final String str = intent.getStringExtra("message");
Intent myIntent = new Intent(getApplicationContext(),Puente2.class);
myIntent.putExtra("dni", str);
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("0-2")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Has elegido solo las 50%");
builder.setMessage("Has elegido 30 Diciembre y 2,3 enero! Queres elegir ahora los de 33%? ");
// add a button
builder.setPositiveButton("Si",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
texts = (TextView)findViewById(R.id.texts);
Intent intent = getIntent();
final String str = intent.getStringExtra("message");
Intent myIntent = new Intent(getApplicationContext(),Puente2.class);
myIntent.putExtra("dni", str);
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
// you to understand error returned from doInBackground method
if(result.equals("1-0")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Has elegido solo las 33%");
builder.setMessage("Has elegido 15,16,17 Abril! Queres elegir ahora los de 50%? ");
// add a button
builder.setPositiveButton("Si",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
texts = (TextView)findViewById(R.id.texts);
Intent intent = getIntent();
final String str = intent.getStringExtra("message");
Intent myIntent = new Intent(getApplicationContext(),Puente3.class);
myIntent.putExtra("dni", str);
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals(" 1-1")) {
Toast.makeText(MainPageActivity.this, result.toString(), Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Todo corecto");
builder.setMessage("Has elegido 15,16,17 Abril y 23,26,27 Diciembre! ");
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("1-2")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Todo corecto");
builder.setMessage("Has elegido 15,16,17 Abril y 30 Diciembre 2,3 Enero! ");
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("2-0")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Falta elegir 50%");
builder.setMessage("Has elegido 29,30 abril y 3 Mayo! Queres elegir ahora los de 50%? ");
// add a button
builder.setPositiveButton("Si",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
texts = (TextView)findViewById(R.id.texts);
Intent intent = getIntent();
final String str = intent.getStringExtra("message");
Intent myIntent = new Intent(getApplicationContext(),Puente2.class);
myIntent.putExtra("dni", str);
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("2-1")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Todo Correcto");
builder.setMessage("Has elegido 29,30Abril y 3 Mayo! Y 23,26,27 Diciembre! ");
// add a button
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("2-2")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Todo Correcto!");
builder.setMessage("Has elegido 29,20 Abril y 3 Mayo! Queres elegir ahora los de 50%? ");
// add a button
builder.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("3-0")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Falta elegir 50%");
builder.setMessage("Has elegido 2,3,4,5 Diciembre Abril! Queres elegir ahora los de 50%? ");
// add a button
builder.setPositiveButton("Si",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
texts = (TextView)findViewById(R.id.texts);
Intent intent = getIntent();
final String str = intent.getStringExtra("message");
Intent myIntent = new Intent(getApplicationContext(),Puente2.class);
myIntent.putExtra("dni", str);
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("3-1")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Todo correcto");
builder.setMessage("Has elegido 2,3,4,5 Diciembre ! 30 Diciembre 2 y 3 Enero! ");
// add a button
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
if(result.equals("3-2")) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainPageActivity.this);
builder.setTitle("Todo correcto");
builder.setMessage("Has elegido 2,3,4,5 Diciembre ! 30 Diciembre 2 y 3 Enero! ");
builder.setPositiveButton("ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}else{
result = result.replace(" " , "");
// you to understand error returned from doInBackground method
Toast.makeText(MainPageActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Php
<?php
$servername = "localhost";
$username = "g";
$password = "s*";
$dbname = "n";
$dni = $_GET['dni'];
/// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM `users` WHERE `username`= '$dni'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "" . $row["puente33"]. "-" . $row["puente50"]. "";
}
}
mysqli_close($conn);
?>
la respuesta en php es 1-1
Pero si borro todo el código php y pongo solo echo"1-1"
; funciona correcto.
Gracias por todo