【发布时间】:2017-02-08 12:21:31
【问题描述】:
我想在我的应用程序中实现搜索功能。即我想根据用户输入的字符串打印到一些特定的数据。
这是我的java代码:
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"));
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
});
}
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");// this is variable and hardcoded passed value for testing
return params;
}
}
;
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
这是我的 php 代码:
<?php
header("content-type:application/json");
require_once("dbConnect.php");
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['fullname'])){
$fullname = $_POST['fullname'];
$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);
}
}
?>
我正在使用 volley 库,我正在尝试在变量中传递一个硬编码值,并尝试在这样的 php 代码中使用它:
params.put("fullname", "test");//here fullname is variable and test is hardcoded value
但是变量中的硬编码传递值没有进入 php 变量(同名),这就是为什么所需的输入没有保存在 php 变量中并且我的搜索不起作用。那么如何解决这个问题呢?
【问题讨论】:
-
to pass a hard coded value in the variable。你认为你不必告诉你在谈论哪个变量?didn't get in php variable。你认为你不必告诉哪个变量? -
请看我更新的代码,你还需要其他信息吗?
-
你应该让 php 脚本总是回显一些 json 文本。现在,如果“如果”之一没有实现,那么您就不会这样做。
-
而且你不应该在第一次测试时尝试插入。只需制作一个 php 脚本来回显一些预定义的 json 以查看您是否可以接收它。专注于呼应变量值。
-
How to perform search in android app using php script。对您的问题的描述非常糟糕。您不知道如何将值发布到我认为的网络服务器。
标签: java php android http-post android-volley