使用我原来的PHP网络服务public void onResponse(String response)
,response
包含以下响应:
{"成功":1,"消息":"现货成功添加!"}
使用我更新的PHP Web服务,response
为空,不存在。
在我的浏览器中运行PHP Web服务时,我得到以下JSON对象:
{"成功":1,"消息":"现货成功添加!"," like_id":" 428"}
这很棒,但我希望response
在应用中看起来一样。
那么:原因是什么,以及如何让JSON对象出现在response
中?
Java代码
public void writeLikedPos(LatLng location) {
double latitude;
double longitude;
try {
latitude = location.latitude; //nullpointer
longitude = location.longitude;
} catch (NullPointerException e) {
System.out.println("Noe gikk galt!");
e.printStackTrace();
return;
}
final String latitudeStr = String.valueOf(latitude);
final String longitudeStr = String.valueOf(longitude);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("String response: " +response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error");
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("latitude", latitudeStr);
params.put("longitude", longitudeStr);
return params;
}
};
VolleySingleton.getInstance().addToReqQueue(postRequest);
}
}
新的网络服务PHP(结果未显示在response
中)
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
//initial query
if(isset($_POST['user_id']) && !empty($_POST['user_id'])) {
$query = "INSERT INTO spot ( latitude, longitude, user_id ) VALUES ( :lat, :long, :uid ) ";
//Update query
$query_params = array(
':lat' => $_POST['latitude'],
':long' => $_POST['longitude'],
':uid' => $_POST['user_id']
);
} else {
$query = "INSERT INTO spot ( latitude, longitude ) VALUES ( :lat, :long ) ";
//Update query
$query_params = array(
':lat' => $_POST['latitude'],
':long' => $_POST['longitude']
);
}
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add lat/long-pair!";
die(json_encode($response));
}
$latt = $_POST['latitude'];
$longg = $_POST['longitude'];
$getId = "SELECT id FROM spot WHERE latitude=$latt AND longitude=$longg LIMIT 1";
//execute query
try {
$stmt2 = $db->prepare($getId);
$result2 = $stmt2->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't retrieve like_id!";
die(json_encode($response));
}
$row = $stmt2->fetchAll();
if($row) {
$response["success"] = 1;
$response["message"] = "Spot Successfully Added!";
$response["like_id"] = $row[0]['id'];
echo json_encode($response);
}
} else {
?>
<h1>Registrer GPS-koordinater</h1>
<form action="idtest.php" method="post">
Latitude:<br />
<input type="text" name="latitude" value="" />
<br /><br />
Longitude:<br />
<input type="text" name="longitude" value="" />
<br /><br />
Google ID:<br />
<input type="text" name="user_id" value="" />
<br /><br />
<input type="submit" value="Opprett kolonne i 'spot'" />
</form>
<?php
}
?>
原始网络服务PHP:
<?php
// TODO: Lag en sjekk for at lat/long faktisk er lat/long-verdier.
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
//initial query
if(isset($_POST['user_id']) && !empty($_POST['user_id'])) {
$query = "INSERT INTO spot ( latitude, longitude, user_id ) VALUES ( :lat, :long, :uid ) ";
//Update query
$query_params = array(
':lat' => $_POST['latitude'],
':long' => $_POST['longitude'],
':uid' => $_POST['user_id']
);
} else {
$query = "INSERT INTO spot ( latitude, longitude ) VALUES ( :lat, :long ) ";
//Update query
$query_params = array(
':lat' => $_POST['latitude'],
':long' => $_POST['longitude']
);
}
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add lat/long-pair!";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Spot Successfully Added!";
echo json_encode($response);
} else {
?>
<h1>Registrer GPS-koordinater</h1>
<form action="addspottest.php" method="post">
Latitude:<br />
<input type="text" name="latitude" value="" />
<br /><br />
Longitude:<br />
<input type="text" name="longitude" value="" />
<br /><br />
Google ID:<br />
<input type="text" name="user_id" value="" />
<br /><br />
<input type="submit" value="Opprett kolonne i 'spot'" />
</form>
<?php
}
?>
答案 0 :(得分:0)
我很惊讶脚本为什么在Web浏览器中显示正确的JSON对象,但在Volley onResponse
中没有任何内容。
在New web service PHP
中实现目标的更好方法是只执行一个查询,Original web service PHP
中的查询,并使用
mysqli_insert_id()
或
PDO::lastInsertId()
取决于您用于MySql连接的方法。