2017-05-03 5 views
-12

英語の間違いがありましたら申し訳ありません。結果がif文と一致する場合でもAndroidは 'else'になります

今、私のアプリはelseのオプションに行くつもりですが、他の場合でも、文にマッチすると、私のコードを見てください。

PHP:

<?php 
error_reporting(0); 
include 'conexao.php'; 

$usuario = $_POST["username"]; 
$senha = $_POST["password"]; 

$result = mysql_query("select * from login where usuario = '" . $usuario . "' and senha = '" . $senha . "';"); 
$num_rows = mysql_num_rows($result); 

if ($num_rows == 0) { 
    echo 'false'; 
} else if ($num_rows == 1) { 
    echo 'true'; 
} else { 
    echo 'nenhum'; 
} 
?> 

と私のJava:

EditText txtusuario, txtsenha; 
    Button btnlogin; 
    public static final int CONNECTION_TIMEOUT=10000; 
    public static final int READ_TIMEOUT=15000; 

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

    txtusuario = (EditText) findViewById(R.id.txtusuario); 
    txtsenha = (EditText) findViewById(R.id.txtsenha); 
    btnlogin = (Button) findViewById(R.id.btnlogin); 

    btnlogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Get text from email and passord field 
      final String username = txtusuario.getText().toString(); 
      final String password = txtsenha.getText().toString(); 

      // Initialize AsyncLogin() class with email and password 
      new AsyncLogin().execute(username,password); 

     } 
    }); 


} 
private class AsyncLogin extends AsyncTask<String, String, String> 
{ 
    ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this); 
    HttpURLConnection conn; 
    URL url = null; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     //this method will be running on UI thread 
     pdLoading.setMessage("\tLoading..."); 
     pdLoading.setCancelable(false); 
     pdLoading.show(); 

    } 
    @Override 
    protected String doInBackground(String... params) { 
     try { 

      // Enter URL address where your php file resides 
      url = new URL("http://rhynotcc.hol.es/teste/login.php"); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return "exception"; 
     } 
     try { 
      // Setup HttpURLConnection class to send and receive data from php and mysql 
      conn = (HttpURLConnection)url.openConnection(); 
      conn.setReadTimeout(READ_TIMEOUT); 
      conn.setConnectTimeout(CONNECTION_TIMEOUT); 
      conn.setRequestMethod("POST"); 

      // setDoInput and setDoOutput method depict handling of both send and receive 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      // Append parameters to URL 
      Uri.Builder builder = new Uri.Builder() 
        .appendQueryParameter("username", params[0]) 
        .appendQueryParameter("password", params[1]); 
      String query = builder.build().getEncodedQuery(); 

      // Open connection for sending data 
      OutputStream os = conn.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8")); 
      writer.write(query); 
      writer.flush(); 
      writer.close(); 
      os.close(); 
      conn.connect(); 

     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
      return "exception"; 
     } 

     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 "exception"; 
     } finally { 
      conn.disconnect(); 
     } 


    } 

    @Override 
    protected void onPostExecute(String result) { 

     //this method will be running on UI thread 

     pdLoading.dismiss(); 
     String teste = "true"; 

     if(result.equals(teste)){ 
     //Notificação para saber o resultado do login 
     int a = 0; 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(LoginActivity.this); 
     mBuilder.setSmallIcon(R.drawable.escavadeira); 
     mBuilder.setContentTitle("Notificação"); 
     mBuilder.setContentText("Conexão deu certo caralho" + result); 
     NotificationManager mNot = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNot.notify(a, mBuilder.build()); 
     } 
     else{ 
      int a = 0; 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(LoginActivity.this); 
      mBuilder.setSmallIcon(R.drawable.escavadeira); 
      mBuilder.setContentTitle("Notificação"); 
      mBuilder.setContentText("Deu errado" + result); 
      NotificationManager mNot = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      mNot.notify(a, mBuilder.build()); 
     } 





    } 

} 
} 

真のif文でしなければならない、と私はすでにresult.equals("true")

を試したとき、私の通知の結果は 'DEU errado本当'
+0

なぜ人々が新しいSOメンバーを助けることができないのか分かりません... put結果を出力します。 –

+0

少しの例がありますか? onPostExecute内の – Raphael

+0

、結果パラメータの値は? –

答えて

0
@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    //this method will be running on UI thread 

    pdLoading.dismiss(); 
    String teste = "true"; 

    int a = 0; 
    NotificationCompat.Builder mBuilder = new 
    NotificationCompat.Builder(LoginActivity.this); 
    mBuilder.setSmallIcon(R.drawable.escavadeira); 
    mBuilder.setContentTitle("Notificação"); 

    if(result.equals(teste)){ 
    //Notificação para saber o resultado do login 
     mBuilder.setContentText("Conexão deu certo caralho" + result); 
    }else{ 
     mBuilder.setContentText("Deu errado" + result); 
    } 

    NotificationManager mNot = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNot.notify(a, mBuilder.build()); 
} 
+0

は、 'deu errado true'を表示しています。これはelseステートメントです: – Raphael

関連する問題