Android 如何通过截取登录表单从服务器发送和接收数据
我想向服务器发送用户名和密码,并且我想通过截击从服务器接收另一个用户的信息。你能帮我吗 我想发送stringrequest并接收jsonarrayrequest,但我现在不知道怎么做 这是我的php代码Android 如何通过截取登录表单从服务器发送和接收数据,android,android-volley,Android,Android Volley,我想向服务器发送用户名和密码,并且我想通过截击从服务器接收另一个用户的信息。你能帮我吗 我想发送stringrequest并接收jsonarrayrequest,但我现在不知道怎么做 这是我的php代码 if($_SERVER['REQUEST_METHOD']=='POST'){ //Getting values $username = $_POST['email']; $password = $_POST['password']; header(
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();
}
您可以发送包含用户名和密码的JSONObject,接收包含用户信息或错误消息的JSONObject。使用截击,如下所示:
JSONObject loginJSon = new JSONObject();
//then put your parameter to this jsonobejct,maybe username and password
//loginJson.put("username",<username>)
this.requestQueue.add(new JsonObjectRequest(<URL_LOGIN>,//your login url
loginJson, new Listener<JSONObject>() {
public void onResponse(JSONObject easyJsonObject) {
//here can get response from your server,just take the value as its key
String uid = easyJsonObject.getString("uid"));
}
}, this));
JSONObject loginJSon=new JSONObject();
//然后将您的参数放入这个jsonobejct,可能是用户名和密码
//loginJson.put(“用户名”,)
this.requestQueue.add(新建JsonObjectRequest(,//您的登录url
loginJson,新的监听器(){
public void onResponse(JSONObject易用JSONObject){
//这里可以从您的服务器获得响应,只需将该值作为其键即可
stringuid=easyJsonObject.getString(“uid”);
}
}这),;
您可以发送包含用户名和密码的JSONObject,接收包含用户信息或错误消息的JSONObject。使用截击,如下所示:
JSONObject loginJSon = new JSONObject();
//then put your parameter to this jsonobejct,maybe username and password
//loginJson.put("username",<username>)
this.requestQueue.add(new JsonObjectRequest(<URL_LOGIN>,//your login url
loginJson, new Listener<JSONObject>() {
public void onResponse(JSONObject easyJsonObject) {
//here can get response from your server,just take the value as its key
String uid = easyJsonObject.getString("uid"));
}
}, this));
JSONObject loginJSon=new JSONObject();
//然后将您的参数放入这个jsonobejct,可能是用户名和密码
//loginJson.put(“用户名”,)
this.requestQueue.add(新建JsonObjectRequest(,//您的登录url
loginJson,新的监听器(){
public void onResponse(JSONObject易用JSONObject){
//这里可以从您的服务器获得响应,只需将该值作为其键即可
stringuid=easyJsonObject.getString(“uid”);
}
}这),;
试试看{
JSONArray JSONArray=新JSONArray(响应);
for(int i=0;i
试试看{
JSONArray JSONArray=新JSONArray(响应);
for(int i=0;i
请解释您尝试了什么以及遇到了什么具体问题。我使用POST方法将用户名和密码发送到php文件,如果该用户有效,在json代码中返回了此用户的另一个字段。如何获取此jsonarray代码?请解释您尝试了什么以及遇到了哪些具体问题。如果此用户有效,我会使用POST方法将用户名和密码发送到php文件,在json代码中向我返回了此用户的另一个字段。我如何获取此jsonarray代码?但我收到jsonarray此行中有错误。stringuid=easyJsonObject.getString(“uid”);我不能使用字符串。但我收到JSONARRAY这行有错误。stringuid=easyJsonObject.getString(“uid”);我不会用字符串。