我尝试发送一个字符串并返回json对象数组作为响应,如下所示:
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.optString("fullname"));
movie.setThumbnailUrl(obj.optString("image"));
movie.setRating(obj.optString("location"));
movie.setYear(obj.getInt("id"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
// adapter.notifyDataSetChanged();
adapter.reloadData();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("fullname", "test"); // fullname is variable and test is value.
return params;
} };在上面代码中,fullname是变量,test是value,使用该变量,我尝试在php变量中发送变量数据并执行查询,如下所示:
<?php
header("content-type:application/json");
require_once("dbConnect.php");
$fullname = $_REQUEST["fullname"];
//echo $fullname."11";
$sql = "SELECT id ,image,fullname,location from uploadfinding WHERE fullname like '%$fullname%'";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result, array(
"id"=>$row["id"],
"fullname"=>$row["fullname"],
"image"=>$row['image'],
"location"=>$row["location"]));
//echo " over";
}
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($result));
fclose($fp);
echo json_encode($result);
mysqli_close($conn);
?>但是我的值测试没有在php变量$fullname中传递。所以问题是如何将值测试转移到php变量$fullname。
发布于 2017-02-09 23:57:12
如果你想在PHP脚本中接收测试数据,那么使用下面的代码。您需要确保请求方法的类型。
<?php
header("content-type:application/json");
require_once("dbConnect.php");
if($_SERVER['REQUEST_METHOD']=='POST')
{
$fullname = $_POST["fullname"];
echo "Received fullname = ".$fullname;
}
else if($_SERVER['REQUEST_METHOD']=='GET')
{
$fullname = $_POST["fullname"];
echo "Received fullname = ".$fullname;
}
$sql = "SELECT id ,image,fullname,location from uploadfinding WHERE fullname like '%$fullname%'";
$res = mysqli_query($conn,$sql);
if($res)
{
$result = array();
$result["detial"] = array();
while($row = mysqli_fetch_array($res)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["fullname"] = $row["fullname"];
$tmp["image"] = $row["image"];
$tmp["location"] = $row["location"];
$tmp["des"] = $row["ProductDescription"];
// push category to final json array
array_push($result["detial"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');
// echoing json result
echo json_encode($result);
}
else
{
echo "No date found ";
}
?>有关更多详细信息,请查看this教程并阅读this 和this文档。
我希望这会对你有所帮助,你会得到答案。
https://stackoverflow.com/questions/42130380
复制相似问题