2016-05-04 9 views
0

Ich möchte Login mit JSON machen. Dies ist mein Login-Web-Service und meine Login-Aktivität in Android Studio. Wenn ich versuche mich anzumelden, antwortet es immer 1. Es bedeutet immer Erfolg. Obwohl der Benutzername und das Passwort falsch sind.Einloggen in Android mit Web-Service PHP

Können Sie mir sagen, warum ist das passiert?

Für die Datenbank verwende ich mysql

login.php

<?php 
$configs = include('config.php'); 

    //echo $configs['user']; 

    $dbhost = $configs['host']; 
    $dbuser = $configs['user']; 
    $dbpass = $configs['pass']; 
    $dbname = $configs['dbname']; 

mysql_connect($dbhost,$dbuser,$dbpass); 
    mysql_select_db($dbname); 

    // array for JSON response 
    $response = array(); 

    // check for required fields 
    if (isset($_POST['username']) && isset($_POST['password'])) { 
     $username = $_POST['username']; 
     $password = $_POST['password']; 
     // include db connect class 
     mysql_connect($dbhost); 
     mysql_select_db($dbname); 
     // mysql inserting a new row 
     $kueri = " SELECT * FROM tbl_customer WHERE username = '$username' AND password = '$password' "; 
     $query = ''; 
     $kueri.=$query; 
     $result = mysql_query($kueri); 

     //$result = mysql_fetch_array($query); 

     // check if row inserted or not 
     if (!empty($result)) { 
     // successfully inserted into database 
     $response["sukses"] = 1; 
     $response["pesan"] = "login sukses"; 
     // echoing JSON response 
     echo json_encode($response); 
     } else { 
     // failed to insert row 
     $response["sukses"] = 0; 
     $response["pesan"] = "Oops! An error occurred."; 
     // echoing JSON response 
     echo json_encode($response); 
     } 
    } else { 
     // required field is missing 
     $response["sukses"] = 0; 
     $response["pesan"] = "Required field(s) is missing"; 
     // echoing JSON response 
     echo json_encode($response); 
    } 
?> 

LoginActivity.java

public class LoginActivity extends AppCompatActivity { 

private static final String URL_TEST = "http://lomapod.azurewebsites.net/login.php"; 
private static final String TAG_PESAN = "sukses"; 


ArrayList<HashMap<String, String>> tempList; 

JSONParser jParser = new JSONParser(); 

private Toolbar toolbar; 
private EditText inputName, inputPassword; 
private TextInputLayout inputLayoutName, inputLayoutPassword; 
private Button btnLogin; 
private TextView txtSignUp,txtUser,txtPass; 
SessionManagement session; 

ProgressDialog pDialog; 

class AttemptLogin extends AsyncTask<String, String, String> { 

    int success; 
    String xuser, xpassword; 

    public AttemptLogin(String xuser, String xpassword) { 
     this.xuser=xuser; 
     this.xpassword=xpassword; 
     pDialog = new ProgressDialog(LoginActivity.this); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog.setMessage("Loading . . ."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    protected String doInBackground(String... args) { 

     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("username", xuser)); 
     params.add(new BasicNameValuePair("password", xpassword)); 

     JSONObject json = jParser.makeHttpRequest(URL_TEST, "POST", params); 
     Log.d("Response: ", json.toString()); 
     try { 

      success = json.getInt(TAG_PESAN); 

      if (success == 1) { 
       Log.d("Successfully Login!", json.toString()); 
       session.createLoginSession(xuser,xpassword); 
       Intent i = new Intent(LoginActivity.this,MainActivity.class); 
       startActivity(i); 
       return json.getString(TAG_PESAN); 
      } 
      else 
      { 
       return json.getString(TAG_PESAN); 
      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String file_url) { 

     pDialog.dismiss(); 
     if (success == 1) { 
      Toast.makeText(LoginActivity.this, "Success Login ", Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 
      Toast.makeText(LoginActivity.this, "Username & Password is wrong", Toast.LENGTH_SHORT).show(); 
     } 

    } 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    session =new SessionManagement(LoginActivity.this); 


    inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name); 
    inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password); 
    inputName = (EditText) findViewById(R.id.input_name); 
    inputPassword = (EditText) findViewById(R.id.input_password); 
    btnLogin = (Button) findViewById(R.id.btnLogin); 
    txtSignUp =(TextView) findViewById(R.id.txtSignUp); 

    inputName.addTextChangedListener(new MyTextWatcher(inputName)); 
    inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword)); 

    // Inflate the layout for this fragment 

    ClikLogin(); 
    ClickSignUp(); 

} 

public void ClikLogin() 
{ 
    btnLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      submitForm(); 
     } 
    }); 
} 

public void ClickSignUp() 
{ 
    txtSignUp.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i =new Intent(LoginActivity.this, SignUpActivity.class); 
      startActivity(i); 
     } 
    }); 
} 


private void submitForm() { 
    if (validateName() && validatePassword()) { 
     new AttemptLogin(inputName.getText().toString(),inputPassword.getText().toString()).execute(); 
    } 

    Toast.makeText(LoginActivity.this, "Thank You!", Toast.LENGTH_SHORT).show(); 
} 

private boolean validateName() { 
    String user=inputName.getText().toString(); 
    if (user==null||user.equals("")) { 
     inputLayoutName.setError(getString(R.string.err_msg_name)); 
     requestFocus(inputName); 
     return false; 
    } else { 
     inputLayoutName.setErrorEnabled(false); 
    } 

    return true; 
} 


private boolean validatePassword() { 
    String pass=inputPassword.getText().toString(); 
    if (pass==null||pass.equals("")) { 
     inputLayoutPassword.setError(getString(R.string.err_msg_pass)); 
     requestFocus(inputPassword); 
     return false; 
    } else { 
     inputLayoutPassword.setErrorEnabled(false); 
    } 

    return true; 
} 


private void requestFocus(View view) { 
    if (view.requestFocus()) { 
     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    } 
} 

private class MyTextWatcher implements TextWatcher { 

    private View view; 

    private MyTextWatcher(View view) { 
     this.view = view; 
    } 

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
    } 

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
    } 

    public void afterTextChanged(Editable editable) { 
     switch (view.getId()) { 
      case R.id.input_name: 
       validateName(); 
       break; 
      case R.id.input_password: 
       validatePassword(); 
       break; 
     } 
    } 
} 
} 
+0

Ich denke, Sie müssen Ergebnis des Dienstes http://lomapod.azurewebsites.net/login.php von Web-Browes –

+0

@DT-Chef testen es ist POST-Methode kann nicht auf Browser testen, wird es immer 0 auf Browser zurückgeben. – AkshayP

+0

Sie müssen den Hash-Code für den Benutzernamen eingeben und vor dem Test bestehen. –

Antwort

1

Das erste, was mit mysql_* Funktionen stoppen. Fangen Sie an, mit mysqli zu arbeiten.

sucht Statt Ergebnis Prüfung für die Anzahl der Zeilen im Ergebnis wie unter

$sql="SELECT * FROM tbl_customer WHERE username = '$username' AND password = '$password' "; 
if ($result=mysqli_query($con,$sql)) 
{ 
    $rowcount=mysqli_num_rows($result); 
    if ($rowcount>0) { 
     // successfully inserted into database 
     $response["sukses"] = 1; 
     $response["pesan"] = "login sukses"; 
     // echoing JSON response 
     echo json_encode($response); 
     } 
+0

Es halten Show Login fehlgeschlagen, was ist $ con? –

+0

funktioniert es. . Danke –

0

con $ = mysqli_connect ("localhost", "my_user", "my_password", "my_db");