【问题标题】:How to i communicate with this api using volley [duplicate]我如何使用 volley 与此 api 进行通信 [重复]
【发布时间】:2019-02-15 14:15:07
【问题描述】:

我在 phpmyadmin 上有以下 api。现在我想学习如何使用 volley lib 发送电子邮件和密码(字符串值)。

<?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;
                }

【问题讨论】:

标签: android api android-volley


【解决方案1】:

您可以使用 volley 发出 stringRequest。

确保您在清单中添加了访问 Internet 权限

使用权限 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);

【讨论】:

  • 应将 JSON 内容标头添加到请求中
【解决方案2】:

此外,在您的 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 注入,并且您的代码更安全。

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2021-02-20
    • 2012-05-26
    相关资源
    最近更新 更多