私はアンドロイド開発に不慣れで、モバイルアプリにログイン機能を持たせるために構築したコードがいくつかあります。入力テキストの入力データの値を mysql データベースと比較しようとしていますが、要点を理解していないようです。コードが私を何かに導いているのか、それとも単にナンセンスなのか、本当にわかりません。ここで私を助けてくれますか?ちなみに、ボタンをクリックしても結果が表示されないだけで、エラーは発生しません。
これは私のコードです:
**
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Edit Text
inputEmail = (EditText) findViewById(R.id.inputEmail);
inputPassword = (EditText) findViewById(R.id.inputPassword);
// Create button
Button btnSubmit = (Button) findViewById(R.id.btnLogin);
// button click event
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CheckLogin().execute();
}
});
}
class CheckLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Main.this);
pDialog.setMessage("Logging in..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String eadd = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/TheCalling/log_in.php");
try {
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("eadd", eadd));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200){
entity = response.getEntity();
if(entity != null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
String inputEmail = jsonResponse.getString("eadd");
String inputPassword = jsonResponse.getString("password");
if(eadd.equals(inputEmail) && password.equals(inputPassword)){
SharedPreferences sp = getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("eadd", eadd);
spedit.putString("password", password);
spedit.commit();
Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Invalid login details", Toast.LENGTH_SHORT).show();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
**
これは私のphpファイルです:
**
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "sample_login";
$response = array();
$db_connect = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
$db = mysql_select_db($db_name);
$eadd = $_POST['eadd'];
$password = $_POST['password'];
// mysql inserting a new row
$result = mysql_query("SELECT * FROM users WHERE eadd = '$eadd' and password = '$password'");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Login succes";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
?>
**