网页发送请求到服务器,通过使用JSON发送HTTP请求到PHP网页服务器,在机器人(sending HTTP reques...

本文档描述了一个Android开发者尝试通过PHP服务器实现Web服务,进行JSON格式的登录请求。Android代码中,使用HttpPost发送包含用户名和密码的JSON对象到服务器,期望收到包含登录状态和用户数据的响应。然而,实际收到的是空响应。PHP服务器端代码展示了如何处理请求并返回登录成功或失败的信息。问题在于Android应用没有收到预期的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我希望所有人都做得很好。 我想添加Web服务是Android应用程序,为了这个,我已经创建了PHP服务器。 请求和响应是在JSON甲酸盐。 和请求类型是“后”这是我的要求甲:

{"method_name":"checkLogin","body": {"username":"someone@some.com","password":"password","device_type":"android","lat":"","long":""}}

预期的响应:

[{"status":"1","message":"Login successfully","data":{"id":"xx","username":"someone@some.com","first_name":"xxx","last_name":"yyy","gender":"xxx","relationship":"xxxx","birthdate":"xxxx","hometown":"","major":null,"class":null,"house":null,"device_type":"iphone","current_location_lat":"0.0","current_location_lon":"0.0","profile_image":"no_male_user.png","is_login":"1","created":"2012","status":"1","tag":[{"id":"x","tag_name":"xxxx"},{"id":"x","tag_name":"xxx xxx"}]}}]

我的Android代码:

userName = userNameField.getText().toString();

passWord = passWordField.getText().toString();

try {

JSONObject json = new JSONObject();

json.put("username", userName);

json.put("password", passWord);

json.put("device_type", "Android");

json.put("lat", "0.0");

json.put("long", "0.0");

JSONObject json1 = new JSONObject();

json1.put("method_name", "checkLogin");

json1.putOpt("body", json);

HttpParams httpParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParams,

TIMEOUT_MILLISEC);

HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

HttpClient client = new DefaultHttpClient(httpParams);

HttpPost request = new HttpPost(URL);

request.setEntity(new StringEntity(json1.toString()));

ResponseHandler responseHandler = new BasicResponseHandler();

String response = client.execute(request, responseHandler);

Toast.makeText(this, response, Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Throwable t) {

Toast.makeText(this, "Request failed: " + t.toString(),

Toast.LENGTH_LONG).show();

}

}

main.php

require_once('configure.php');

require_once('db.php');

require_once("request.php");

require_once("error_msg.php");

require_once("./includes/JSON.php");

$json = $_POST['json'];

$objRequest = new Request($json);

$rows = $objRequest -> main();

echo $rows;

?>

request.php

require_once("./includes/JSON.php");

require_once("db.php");

require_once('SimpleImage.php');

class Request

{

function __construct($json)

{

$this -> array1 = @json_decode(stripcslashes(trim($json)));

}

function main()

{

$objDb = new DB();

$conn = $objDb -> getConnection();

if(!$conn)

return Error :: $DB_CONN_ERROR." ".$DB_CONN_ERROR_DESC;

else

{

if($this -> array1 -> method_name == 'checkLogin')

return $this -> getLoginFlag($this -> array1 -> body);

}

}

// For Login Start

function getLoginFlag($body)

{

$username = trim($body -> username);

$password = ($body -> password);

$device_type = trim($body -> device_type);

$this -> rs_login = DB :: selectLoginRecord($body);

$arr_resp = array();

$retArray = array();

$tagListArray = array();

if(mysql_num_rows($this -> rs_login) > 0)

{

while($rows = mysql_fetch_array($this -> rs_login))

{

$arr_resp = array("id"=> $rows['id'],"username"=> $rows['username'],"first_name"=> ucfirst($rows['first_name']),"last_name"=>ucfirst($rows['last_name']),"gender"=>$rows['gender'],"relationship"=>$rows['relationship'],"birthdate"=>date('M d, Y', strtotime($rows['birthdate'])),"hometown"=>$rows['hometown'],"major"=>$rows['major'],"class"=>$rows['class'],"house"=>$rows['house'],"device_type"=>$rows['device_type'],"current_location_lat"=>$rows['current_location_lat'],"current_location_lon"=>$rows['current_location_lon'],"profile_image"=>$rows['profile_image'],"is_login"=>$rows['is_login'],"created"=>date('Y', strtotime($rows['created'])),"status"=>$rows['status']);

}

$this -> rs_tag_list = DB :: selectTagList($arr_resp['id']);

if(mysql_num_rows($this -> rs_tag_list) > 0)

{

while($rows = mysql_fetch_array($this -> rs_tag_list))

{

$tagListArray['tag'][]= array("id"=> $rows['id'],"tag_name"=> $rows['tag_name']);

}

}

$finalArray = array_merge($arr_resp,$tagListArray);

$retArray[] = array("status" => "1","message" => "Login successfully","data" => $finalArray);

}

else

{

$retArray[] = array("status" => "0","message" => "The username or password you entered is incorrect");

}

return json_encode($retArray);

}

// End Login

}

?>

但是我得到空响应,而预期的响应,你可以帮我如何做到这一点的,谢谢你的帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值