我在phpmyadmin上具有以下api。现在,我想学习如何使用凌空库发送电子邮件和密码(字符串值)。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends MX_Controller {
public function index()
{
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
$email = $obj['email'];
$password = $obj['password'];
if ($obj['email']!="") {
$this->load->module('camps');
$mysql_query = "SELECT * FROM accounts where email='$email' and password='$password'";
$result = $this->camps->_custom_query($mysql_query);
$query = $result->num_rows();
if ($query==0) {
echo json_encode($password);
}
else {
echo json_encode('OK');
}
}
else {
echo json_encode('Try Again');
}
}
}
}
我已经尝试过使用getParams()
这样的方法,但是它不起作用。
注意:uname和密码为EditTexts
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("email",uname.getText().toString());
params.put("password",password.getText().toString());
return params;
}
答案 0 :(得分:0)
您可以使用截击发出stringRequest。
确保您在清单中添加了访问Internet权限
uses-permission android:name =“ android.permission.INTERNET”
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Your_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
parseYourResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Some Error Occurs!", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("username",uname.getText().toString());
params.put("password", password.getText().toString());
return params;
}
};
queue.add(stringRequest);
答案 1 :(得分:0)
此外,在您的Web服务中,您应该使用准备好的查询,并且不要直接在sql查询中使用变量。这是非常不安全的做法。例如:
<?php
$email= $_REQUEST['email'];
$password = $_REQUEST['pass'];
if (isset($email)){
$res=$dbh->prepare("select * from accounts where email=:email and password=:passw ;");
$res->execute(array(':email' => $email, ':passw' => md5($password)));
$datos = array();
foreach ($res as $row) {
$datos[] = $row;
}
echo json_encode($datos);
?>
这样可以避免sql注入,并且代码更安全。