如何通过Volley登录表单

时间:2016-03-18 21:27:28

标签: android android-volley

我想向服务器发送用户名和密码,我想通过Volley从服务器接收其他用户的信息。你可以帮我吗?

我想发送stringrequest并接收jsonarrayrequest,但我现在不知道怎么做。

这是我的PHP代码

if($_SERVER['REQUEST_METHOD']=='POST'){
    //Getting values 
    $username = $_POST['email'];
    $password = $_POST['password'];     
    header( 'Content-Type: application/json; charset=utf-8' );
    mysql_connect("localhost" , "-------" , "--------");//change server name  //pass username according your settings

    mysql_select_db("--------");// also chang the Mysql database name
    mysql_query("SET NAMES 'utf8'"); 
    mysql_query('SET CHARACTER SET utf8'); 
    $sql1=mysql_query("SELECT * FROM tbl_user WHERE email='$username' AND password='$password'");

    if (!$sql1) {

    echo "Could not successfully run query ($sql) from DB: " . mysql_error();

    exit;

    }

    while($row=mysql_fetch_assoc($sql1))

    $output[]=$row;


    //print(json_encode($output, JSON_UNESCAPED_UNICODE));// this will print the output in json
    echo preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($output));

    mysql_close();

}

2 个答案:

答案 0 :(得分:2)

您可以发送包含用户名和密码的JSONObject,并接收包含用户信息或错误消息的JSONObject。使用Volley,如下所示:

def foo():
    """
    Populates the database with 'VALUES'

    >>> import sqlite3
    >>> con = sqlite3.connect('test.db')
    >>> cur = con.cursor()
    >>> cur.execute('select * from users').fetchall()
    [('admin', 'Admin', 1, 'admin123'), \
('foo', 'bar', 2, 'foo123'), \
('john', 'doe', 3, 'john123')]
    >>> 

    """

答案 1 :(得分:0)

 try {
                        JSONArray jsonArray = new JSONArray(response);
                        for(int i = 0; i < jsonArray.length(); i++)
                        {
                            JSONObject jsonObj = jsonArray.getJSONObject(i);
                            if (!jsonArray.isNull(0)){
                                String name=jsonObj.getString("name");

                            }else {
                                //If the server response is not success
                                //Displaying an error message on toast

                            };

                        }
                    } catch (JSONException e) {
                       //error
                    }