简体   繁体   English

如何使用 android volley 将字符串值从应用程序传递到 php web 服务

[英]How to pass String value from a app to php web service using android volley

I want to implement a search feature in my app.ie I want to print to some specific data according to user input as string.我想在我的应用程序中实现一个搜索功能。我想根据用户输入的字符串打印到一些特定的数据。

here is my java code:这是我的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);

Here is my php code:这是我的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);
         }
    }
    ?>

I'm using volley library and I'm trying to pass a hard coded value in the variable and trying to use it on php code like this:我正在使用 volley 库,并尝试在变量中传递一个硬编码值,并尝试在 php 代码中使用它,如下所示:

 params.put("fullname", "test");//here fullname is variable and test is hardcoded  value

but that hard coded passed value in the variable didn't get in php variable (with same name),that's why desired input not saved in php variable and my search is not working.但是变量中的硬编码传递值没有进入 php 变量(具有相同的名称),这就是为什么所需的输入没有保存在 php 变量中并且我的搜索不起作用。 So how to resolve this issue?那么如何解决这个问题呢?

You are doing a JsonArrayRequest.您正在执行 JsonArrayRequest。 But your php script is for a normal POST request.但是您的 php 脚本是针对普通 POST 请求的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM